author:LPL
date:2019/3/29
重复输出字符串
print(‘hello’ * 2)
通过索引取字符串中的字符
print(‘helloadcd’[2:])
判断给定字符是否在字符串中,关键字in
print(‘el’ in ‘hello’)
格式化输出
print(’%s is a pretty girl’ % ‘tiantian’)
字符串拼接
区别于字符串直接用+号连接,直接用+ ,浪费空间
a = ‘abc’
b = ‘1234’
d = ‘dfg’
c = ‘—’.join([a, b, d])
print©
python的内置方法
st = ‘hello kitty {name} is {age}’
print(st.count(‘l’)) # 统计元素个数
print(st.capitalize()) # 首字母大写
print(st.center(50, ‘-’)) # 打印50字符,以st为中心,其余用-补充
print(st.endswith(‘tty’)) # 以某个字符串结尾
print(st.startswith(‘he’)) # 以某个内容开始
print(st.find(‘t’)) # 查找某个元素,并返回索引值
print(st.format(name=‘LPL’, age=22)) # 格式化输出
print(st.format_map({‘name’: ‘LPL’, ‘age’: 23})) # 格式化输出的另一种形式
# print(st.index(a)) # 查找某个元素的索引值,找不到报错
print(‘My Title’.istitle())
print(‘abdDfFG’.lower())
print(‘fjKJKfd’.upper())
print(‘My Title’.swapcase())
print(‘My Title’.ljust(50, ‘’))
print(‘My Title’.rjust(50, '’))
print(’ My Title ‘.strip())
print(’ My Title ‘.lstrip())
print(’ My Title ‘.rstrip())
print(‘My title’.replace(‘title’, ‘lesson’))
print(‘My title title’.rfind(‘t’))
print(‘My title title’.split(’ ‘))
print(’’.join([‘addd’, ‘sssf’, ‘dddd’]))
print(‘My title title’.split(‘i’, 1))
print(‘My title title’.split(‘i’, 2))
print(‘My title title’.title())