Python日记Day_1 字符串,数字操作

《Python编程从入门到实践》

第二章
变量和简单数据类型
前言:不同的引号

print('Hello world!')
print("Hello world!")
print('''Hello world!''')
print("Don't run away from me!")
print('Don't run away from me!)#错误 标识符与语句混乱
#无法识别结束位置!!
Hello world!
Hello world!
Hello world!
Don't run away from me!

1. 字符串

①修改字符串的大小
title,upper,lower
首字母大写(其他都小写),全部大写,全部小写
注意使用时name.title() (小括号不要漏啦)

# 字符串
name = "jackLove"
print(name)
print("name.title()")
print('name.title()')
print(name.title())
print(name.upper())
print(name.lower())

输出为:

jackLove
name.title()
name.title()
Jacklove
JACKLOVE
jacklove

②合并(连接)字符串``

first_name = "jackLove"
First_name = first_name.title()
last_name ="Theshy"
full_name = first_name+' '+last_name
print(full_name)
print(First_name+" "+last_name)

输出为:

jackLove Theshy
Jacklove Theshy

③制表符与换行符

print("LPL competitors:\n\tJacklove\n\tTheshy\n\tFeaker")

输出为:

LPL competitors:
 Jacklove
 Theshy
 Feaker

④删除空白
strip( 剥除),rstrip(右删除),lstrip(左删除)

name="  Theshy  "
print(name)
print(name.rstrip())
print(name.lstrip())
print(name.strip())
  Theshy  #两个空格
  Theshy
Theshy  
Theshy

2. 数字

t_1=2+3
t_2=0.1+4
print(t_1)
print(t_2)
5
4.1

str()避免类型错误
功能:将数值转化为字符串

age=18
message='Today is my'+age+"birthday"

错误提示

Traceback (most recent call last):
  File "D:/Python—Practice/Python_01.py", line 2, in <module>
    message='Today is my'+age+"birthday"
TypeError: can only concatenate str (not "int") to str
age=18
message='Today is my '+str(age)+"rd birthday!"
print(message)

正确运行

Today is my 18rd birthday!

你可能感兴趣的:(python基础)