字符串的格式化输出
%d
print('%d' % 3)
print('%d' % 100)
%f
%.2f 表示保留小数点后两位
print('%.2f' % 3.14)
print('%.2f' % 100.00)
%s
print('%s' % 3.14)
print('%s' % 100)
print('%s' % 'python')
format函数
'{:,}'.format(1234567890)
# 1,234,567,890
'{0}...{1}....{2}'.format(0,1,2)
字符串函数
去除字符串空格函数 strip()
" abc " .strip()
# "abc"
" a b c ".strip()
#"a b c"
#strip()函数不会去除字符串之间的空格 ,只会去除前后空格
lstrip() # 去除左空格
rstrip() # 去除右空格
字符串大小写函数 upper() capitalize() lower() title()
"china".upper() # CHINA
"china".captialize() # China
"CHINA".lower() # china
"i have a dream".title() # I Have A Dream
字符串判断函数 startswith() endswith() isdigit() islower() isupper()
字符串查找与替换函数 in find() index() count() replace()
"abc" in "abcd" # True
"a".find("abcd") # 如果可以找到则返回索引值, 如果找不到则返回-1
"a".index("abcd") # 如果可以找到则返回索引值, 如果找不到则会报a”
"a".count("abcd") # 查找目标字符在字符串中出现次数
"abbca".replace('a', 'c') # 替换目标字符 cbbcc
字符串长度函数 len()
len('abc') # 3
len('') # 0
len('a') # 1