字符串: 可用单引号 双引号 三引号 来表示
可用来定义国籍, 姓名, 家庭住址等
选项:
- #.join 把可迭代的对象转化成字符串 (字符串, 列表, 元组, 字典等), 列表 元组 合并为字符串, 字典合并的是 key
- >>> res = ''.join(['1','2','3']) #列表
- >>> print(res)
- 123
- >>> res = ''.join(('1','2','3')) #元组
- >>> print(res)
- 123
- >>> res = ''.join({'a':1,'b':2})
- >>> print(res)
- ab
- >>> a = ':'.join('1,2,3,4,5'.split(','))
- >>> print(a)
- 1:2:3:4:5
- # 定义 ip str1 = '192.168.200.12'
- #.split 把字符串分割成列表(从左往右分割) rsplit (从右往左分割)
- >> str1 = '192.168.200.12'
- >>> res = str1.split('.')
- >>> print(res)
- ['192', '168', '200', '12']
- >>> str1 = '192.168.200.12'
- >>> res = str1.split('.',1)
- >>> print(res)
- ['192', '168.200.12']
- >>> str1 = '192.168.200.12'
- >>> res = str1.rsplit('.',1)
- >>> print(res)
- ['192.168.200', '12']
- #replace 替换字符串
- >>> str1 = '192.168.200.12'
- >>> res = str1.replace('.','|')
- >>> print(res)
- 192|168|200|12
- >>> str1 = '192.168.200.12'
- >>> res = str1.replace('.','|',1)
- >>> print(res)
- 192|168.200.12
- #strip 去除字符串两边指定字符 rstrip(去除右边) lstrip (去除左边)
- >>> str1 = '==192.168.200.12=='
- >>> res = str1.strip('=')
- >>> print(res)
- 192.168.200.12
- #encode 字符编码
utf8 格式的字符编码: 1 个中文占 3 个字节 生僻字会占用更多
gpk 格式的字符编码: 1 个中文占 2 个字节
用什么字符编码写入就用什么字符编码打开
- >>> str1 = '天明'
- >>> res = str1.encode('utf-8')
- >>> print(res)
- b'\xe5\xa4\xa9\xe6\x98\x8e'
- >>> str1 = '天明'
- >>> result = res.decode('utf-8')
- >>> print(result)
天明
- # 占位符: %s(字符串) %d(整型) %f(浮点型)
- >>> name1 = '天明'
- >>> name2 = 'tianming'
- >>> res = 'my name is %s'
- >>> print(res % name1)
- >>> print(res % name2)
my name is 天明
- my name is tianming
- >>> name1 = '天明'
- >>> res = 'my name is %s' % 123
- >>> print(res)
- my name is 123
- >>> name1 = '天明'
- >>> res = 'my name is %.3f' % 123 #保留三位小数
- >>> print(res)
- my name is 123.000
- #format 格式化输出
- >>> name1 = '天明'
- >>> age = 25
- >>> res = 'my name is {},my age is {}'.format(name1,age)
- >>> print(res)
my name is 天明, my age is 25
- >>> name1 = '天明'
- >>> age = 25
- >>> res = 'my name is {0},my age is {1}'.format(name1,age)
- >>> print(res)
my name is 天明, my age is 25
- >>> name1 = '天明'
- >>> age = 25
- >>> res = 'my name is {name},my age is {age}'.format(name=name1,age=age)
- >>> print(res)
my name is 天明, my age is 25
- name1 = '天明'
- >>> age = 25
- >>> res = f"my name is {name1},my age is {age}"
- >>> print(res)
my name is 天明, my age is 25
- # 字符串可以拼接: 相加 可以与数字相乘
- >>> a = 'abc'
- >>> b = '235'
- >>> print(a+b)
- abc235
- >>> a = 'abc'
- >>> b = '235'
- >>> print(a*5)
- abcabcabcabcabc
- >>> print(b*2)
- 235235
- #[::-1] 反转数组或序列的顺序
- >>> str1 = '123'
- >>> res =str1[::-1]
- >>> print(res)
- 321
- #type 类型
- >>> str1 = '123'
- >>> print(type(str1))
- <class 'str'>
- #id 内存占用
- >>> str1 = '123'
- >>> print(id(str1))
- 61103744
- #isalpha() 判断字符串是否是字母
- >>> str1 = '天明'
- >>> res = str1.isalpha()
- >>> print(res)
- True
- #isalnum() 判断是否是数字或者字符组成
- >>> str1 = '天明'
- >>> res = str1.isalnum()
- >>> print(res)
- True
- #isaldigit() 判断是否是整型
- >>> str1 = '天明'
- >>> res = str1.isdigit()
- >>> print(res)
- False
- >>> str2 = '123123'
- >>> res =str2.isdigit()
- >>> print(res)
- True
- #rfind 从右往左找第一个对应的值 索引是正向的 (0 开始) 找不到就会变成 - 1
- >>> str2 = '123123'
- >>> res = str2.rfind('2')
- >>> print(res)
- 4
- >>> str2 = '123123'
- >>> res = str2.rfind('3',0,1) #范围 0-3 之间 不包括 3 包括 0 找不到就会变成 - 1
- >>> print(res)
- -1
- #find 从左往右找第一个对应的值 索引是正向的(0 开始)
- >>> str2 = '123123'
- >>> res =str2.find('3')
- >>> print(res)
- 2
- #index 从左往右找第一个对应的值 显示正向索引 没有找到匹配的值会报错
- >>> str2 = '123123'
- >>> res =str2.index('2')
- >>> print(res)
- 1
- #count 统计字符个数 如果没有显示 0
- >>> str2 = '123123'
- >>> res = str2.count('2')
- >>> print(res)
- 2
- #title 把字符串变成抬头
- >>> str1 = 'hello word'
- >>> res = str1.title()
- >>> print(res)
- Hello Word
- #startswith 判断字符串当中开头字符是否为所选的字符
- >>> str1 = 'hello word'
- >>> res = str1.startswith('h')
- >>> print(res)
- True
- #endswith 判断字符串当中结尾字符是否为所选的字符
- >>> str1 = 'hello word'
- >>> res = str1.endswith('d')
- >>> print(res)
- True
- #isspace 判断是否是由空格组成
- >>> str1 = ' '
- >>> res = str1.isspace()
- >>> print(res)
- True
来源: http://www.bubuko.com/infodetail-3280921.html