str在python是什么类型
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。
一、字符串的创建
test = str() / ""
test = str("licheng") / "licheng"
无参数,创建空字符串
一个参数,创建普通字符串
两个参数,int(字节,编码)
二、字符串的常用方法
复制代码
string = 'this is a string.'
new_str = string.capitalize()
print(new_str)
string = 'this is a string.'
new_str = string.center(30,'*')
print(new_str)
string = 'this is a string.'
new_str = string.count('i')
print(new_str)
string = 'this is a string.'
new_str = string.decode()
new_str = string.encode()
print(new_str)
string = 'this is a string.'
new_str = string.endswith('ing.')
print(new_str)
string = 'this is a string.'
new_str = string.find('a') #找的到的情况
print(new_str)
new_str = string.find('xx') #找不到的情况返回-1
print(new_str)
string = 'this is a string.'
new_str = string.index('a') #找的到的情况
print(new_str)
new_str = string.index('xx') #找不到的情况,程序报错
print(new_str)
string = 'My name is yue,my age is 18.'
new_str = string.isalnum()
print(new_str)
string = 'haha18121314lala'
new_str = string.isalnum()
print(new_str)
string = 'abcdefg'
new_str = string.isalpha()
print(new_str)
string = 'my name is yue'
new_str = string.isalpha() #字母中间带空格、特殊字符都不行
print(new_str)
string = '1234567890'
new_str = string.isdigit()
print(new_str)
string = 'haha123lala'
new_str = string.isdigit() #中间带空格、特殊字符都不行
print(new_str)
string = 'my name is yue,my age is 18.'
new_str = string.islower()
print(new_str)
string = 'My name is Yue,my age is 18.'
new_str = string.islower()
print(new_str)
string = 'MY NAME IS YUE.'
new_str = string.isupper()
print(new_str)
string = 'My name is Yue.'
new_str = string.isupper()
print(new_str)
string = ("haha","lala","ohoh")
str = "-"
print(str.join(string))
string = "My Name is YUE."
print(string.lower())
string = " My Name is YUE."
print(string.lstrip())
string = "My Name is YUE."
print(string.lstrip('My'))
string = "My name is yue."
print(string.replace("yue","ying"))
string = "My name is yue."
print(string.rfind('is'))
string = "My name is yue."
print(string.rfind('XXX'))
string = "haha lala gege"
print(string.split(' '))
print(string.split(' ', 1 ))
string = "haha lala gege"
print(string.rsplit(' '))
print(string.rsplit(' ', 1 ))
string = " My name is yue. "
print(string.rstrip())
string = " My name is yue. "
print(string.strip())
string = "my name is yue,my age is 18."
print(string.upper())
复制代码
str源码
三、字符串的公共功能
索引(只能取一个元素)
切片(取多个元素)
长度(len)
python2:按字节算长度
python3:按字符算长度
for循环(同长度的版本循环单位)
四、字符与字节的转换
复制代码
s = "李程"
b = bytes(s, encoding="gbk")
type(b) 输出为字节类型
c = str(b, encoding="gbk")
复制代码
五、字符串格式化
Python的字符串格式化有两种方式: 百分号方式、format方式
百分号的方式相对来说比较老,而format方式则是比较先进的方式,企图替换古老的方式,目前两者并存。
1、百分号方式
%(name)[width].[precision]typecode
参数详解
常用格式化:
复制代码
tpl = "i am %s" % "spark"
tpl = "i am %s age %d" % ("spark", 18)
tpl = "i am %(name)s age %(age)d" % {"name": "spark", "age": 18}
tpl = "percent %.2f" % 99.97623
tpl = "i am %(pp).2f" % {"pp": 123.425556, }
tpl = "i am %.2f %%" % {"pp": 123.425556, }
复制代码
2、Format方式
[[fill]align]sign0,[type]
参数详解
常用格式化:
复制代码
1 tpl = "i am {}, age {}, {}".format("seven", 18, 'alex')
2
3 tpl = "i am {}, age {}, {}".format(*["seven", 18, 'alex'])
4
5 tpl = "i am {0}, age {1}, really {0}".format("seven", 18)
6
7 tpl = "i am {0}, age {1}, really {0}".format(*["seven", 18])
8
9 tpl = "i am {name}, age {age}, really {name}".format(name="seven", age=18)
10
11 tpl = "i am {name}, age {age}, really {name}".format(**{"name": "seven", "age": 18})
12
13 tpl = "i am {0[0]}, age {0[1]}, really {0[2]}".format([1, 2, 3], [11, 22, 33])
14
15 tpl = "i am {:s}, age {:d}, money {:f}".format("seven", 18, 88888.1)
16
17 tpl = "i am {:s}, age {:d}".format(*["seven", 18])
18
19 tpl = "i am {name:s}, age {age:d}".format(name="seven", age=18)
20
21 tpl = "i am {name:s}, age {age:d}".format(**{"name": "seven", "age": 18})
22
23 tpl = "numbers: {:b},{:o},{:d},{:x},{:X}, {:%}".format(15, 15, 15, 15, 15, 15.87623, 2)
24
25 tpl = "numbers: {:b},{:o},{:d},{:x},{:X}, {:%}".format(15, 15, 15, 15, 15, 15.87623, 2)
26
27 tpl = "numbers: {0:b},{0:o},{0:d},{0:x},{0:X}, {0:%}".format(15)
28
29 tpl = "numbers: {num:b},{num:o},{num:d},{num:x},{num:X}, {num:%}".format(num=15)