4. 字符串格式化
%s 格式化字符串
%d 格式化整数
%f 浮点数
print("我叫 %s 今年 %d 岁"%('小明, 10))
.format() 格式化
print('{}'.format("hello"))
5. 字符串的一些函数
mystr="hello"
len(mystr) 字符串的长度
count():mystr.count("l") 返回有几个字符
capitalize():mystr.capitalize() 将字符串的第一个字符转换为大写
title():将每一个单词的首字母大写
find():mystr.find("l") 找字符串,返回找到字符串的下标,如果没有返回 - 1
rfind() 从右面开始找
index(): 和 find() 一样,找存在的下标,如果找不到返回异常
rindex(): 从右面开始找
replace():mystr.replace("ll","LL"), 但是原字符串不会被改变
split():mystr.split(" "): 如果 split() 什么都不写,就是将通过 \ t 和空格进行拆分
startswith(): 以 xx 开头
endswith(): 以 XX 结尾
lower(): 转成小写
upper(): 转成大写
ljust(),rjust(),center():字符串输出时怎么对齐,括号里写大小
strip(),lstrip(),rstrip() 去除空格
partition():mystr.partition("XXX") 以此字符串为节点拆分
splitlines(): 按照换行符进行拆分,如果字符串中有"\n"
isalpha(): 判断字符串是否是字母
isdigit(): 判断字符串是否是数字
isalnum(): 判断字符串是否是数字和字母
join(): 将列表组成字符串,mystr.join(" ")
来源: http://blog.csdn.net/sheng95/article/details/78826441