python string函数包括:
1、str.capitalize:将原字符串内的首字母转成大写,其他部分小写,再返回新字符串
print("s.capitalize() = {function}" s.capitalize() = Abcada a
2、str.lower:将原字符串的字母转为小写
print("s.lower() = {function}".format(function = s.lower())) s.lower() = abcada a
3、str.upper:将原字符串的字母转为大写
print("s.upper() = {function}".format(function = s.upper())) s.upper() = ABCADA A
4、str.swapcase:将原字符串的大写小写反转
print("s.swapcase() = {function}".format(function = s.swapcase())) s.swapcase() = ABCAdA A
5、str.title:原字符串内如果有特殊字符(包括数字)连接字母,则将特殊字符后的首个英文字母转化为大写形态,并返回新字符串
print("s2.title() = {function}".format(function = s2.title())) s2.title() = 123A Abc Abcsaa S
6、str.center:str.center(宽度,填充字符) 将字符串以居中的格式返回,若宽度值比len(s)小则返回原字符串,填充以从左到右为规则,填充字符的默认值为空格,值可以自己更改
print("s2.center() = {function}".format(function = s2.center(19,'&'))) print("s2.center() = {function}".format(function = s2.center(20,'&'))) #s2 = 123a abc ABCSAa s s2.center() = &123a abc ABCSAa s s2.center() = &123a abc ABCSAa s &
7、str.expandtabs:str.expandtabs(tabsize = 8) 将原字符串中 以前的字符补满8位(默认),tabsize的值从0-7即8位,在0-7中任意取值则默认tabsize = 8,此后往上+1,就相当于增加一个空格
print("s3.expandtabs ={function}".format(function = s3.expandtabs())) print("s3.expandtabs ={function}".format(function = s3.expandtabs(0))) print("s3.expandtabs ={function}".format(function = s3.expandtabs(5))) print("s3.expandtabs ={function}".format(function = s3.expandtabs(8))) print("s3.expandtabs ={function}".format(function = s3.expandtabs(9)))#s3 = " as b123" s3.expandtabs = as b123 s3.expandtabs =as b123 s3.expandtabs = as b123 s3.expandtabs = as b123 s3.expandtabs = as
除了上述举例的,string函数还有许多实用的函数。
分析如下:
float('30.7894')=30.7894
python中的字符数字之间的转换函数:
扩展资料
Python在设计上坚持了清晰划一的风格,这使得Python成为一门易读、易维护,并且被大量用户所欢迎的、用途广泛的语言。设计者开发时总的指导思想是,对于一个特定的问题,只要有一种最好的方法来解决就好了。这在由Tim Peters写的Python格言(称为The Zen of Python)里面表述为:There should be one-- and preferably only one --obvious way to do it. 这正好和Perl语言(另一种功能类似的高级动态语言)的中心思想TMTOWTDI(There's More Than One Way To Do It)完全相反。
Python的作者有意的设计限制性很强的语法,使得不好的编程习惯(例如if语句的下一行不向右缩进)都不能通过编译。其中很重要的一项就是Python的缩进规则。
一个和其他大多数语言(如C)的区别就是,一个模块的界限,完全是由每行的首字符在这一行的位置来决定的(而C语言是用一对花括号{}来明确的定出模块的边界的,与字符的位置毫无关系)。这一点曾经引起过争议。因为自从C这类的语言诞生后,语言的语法含义与字符的排列方式分离开来,曾经被认为是一种程序语言的进步。不过不可否认的是,通过强制程序员们缩进(包括if,for和函数定义等所有需要使用模块的地方),Python确实使得程序更加清晰和美观。
参考资料:百度百科-python