Python3 字符串用法

it2023-09-27  73

在Python中即可以使用双引号""也可以使用单引号''对字符串进行赋值

例如

name = "hello world"

也可以

name = 'he said "hello world"'

在Python 常见的字符串使用

name.title() :将单词的首字母大写

name.upper():将字符串改为全部大写

name.lower():将字符串改为全部小写

name ='hello world' name = name.title(); #Hello world name = name.upper(); #HELLO WORLD name = name.lowe() #hello world

字符串拼接 :直接用“+”号来合并字符串,但是如果类型一致需要转换

first_name = 'fly' last_name = 'chen' name = first_name +" "+last_name age = 23 message = 'happy'+ age +'23rd birthday' #这样就会出错 python 并不能解读出这个数 因此将INt 转换为str message = 'happy'+ str(age) +'23rd birthday'

 

删除字符串末尾空白

message = 'i like it ' #'i like it ' message = message.restrip()

 

 

最新回复(0)