长字符串
- >>> str = '''
- ---------userinfo----------
- name:lalalaxpf
- age:18
- sex:man
- iphone:123
- address:ooxx
- -----------end--------------
- '''
- >>> print(str)
- ---------userinfo----------
- name:lalalaxpf
- age:18
- sex:man
- iphone:123
- address:ooxx
- -----------end--------------
原始字符串
取消字符的特殊意义
- >>> print('oo\nxx')
- oo
- xx
- >>> print(r'oo\nxx')
- oo\nxx
字符串拼接
1, 通过 + 连接
- >>> print('oo'+'xx')
- ooxx
2,join() 方法
- >>> list = ['a','b']
- >>> ''.join(list)
- 'ab'
字符串切片, 截取
语法
str[start:end:step]
字符串常用方法
- str.strip()
- # 去掉空格和换行符, 中间的去不了
- >>> str = '\noo \nxx\n'
- >>> str.strip()
- 'oo \nxx'
- # 去掉指定字符串
- >>> str = 'ooxx'
- >>> str.strip('xx')
- 'oo'
- str.lstrip(),str.rstrip()
- >>> str = 'ooxx'
- >>> str.lstrip()# 去掉左边的空格和换行符
- 'ooxx'
- >>> str.rstrip()# 去掉右边的空格和换行符
- 'ooxx'
- str.count()
- # 查找某个字符在字符串里面出现的次数
- >>> str = 'ooxx'
- >>> str.count('o')
- 2
- str.capitalize()
- # 首字母大写
- >>> str = 'ooxx'
- >>> str.capitalize()
- 'Ooxx'
- str.title()
- # 所有单词都是以大写开始, 其余字母均为小写
- >>> str = 'hello world'
- >>> str.title()
- 'Hello World'
来源: http://www.bubuko.com/infodetail-2733846.html