代码示例
- 编写脚本test.py
char=" heLLo world "
print(char.title()) #title()使每个单词的首字母大写
print(char.upper()) #upper()将字符串改为全部大写
print(char.lower()) #lower()将字符串改为全部小写
print("\tWa,"+char.title()+"!\n") #利用制表符或换行符添加空白并拼接字符串
print(char.rstrip()) #rstrip()删除字符串末尾空白
print(char.lstrip()) #lstrip()删除字符串开头空白
print(char.strip()) #strip()同时删除字符串两端空白
print("Albert Einstein once said,\
'A person who never make a mistake nerver tried anything new.'")
print(str(20)+char) #str()将非字符串值表示为字符串
print(len(char)) #len()统计字符串长度
print(char.replace("heLLo", "hi")) #替换
print(char.split()) #split()以空格为分隔符将字符串拆成多部分
print(char.count("o")) #count()统计特定字符的出现次数
- 输出结果
Hello World
HELLO WORLD
hello world
Wa, Hello World !
heLLo world
heLLo world
heLLo world
Albert Einstein once said,'A person who never make a mistake nerver tried anything new.'
20 heLLo world
13
hi world
['heLLo', 'world']
2
标签:字符,title,python,char,字符串,print,world,heLLo,大全 From: https://www.cnblogs.com/chaimy/p/17056404.html