Python中字符串常用的方法

来源:互联网 发布:mac连不上wifi 编辑:程序博客网 时间:2024/06/10 18:12

python中字符串常用的方法:

information = "this is shanghai city,"information1 = "It's very bustling"aaa = "I come from {city},and myhometown is {name}"print(information.center(50,'*'))   #长度为50,并且居中print(information.find('s'))        #查找字符串的索引位置,默认是第一个,和index方法类似print(information.startswith('th'))    #判断字符串以什么开头print(information.strip())    #去掉字符串两头的空格和换行符print(information.split('i'))    #去掉字符串两头的空格和换行符print(aaa.format(city='石家庄',name="行唐"))    #字符串的格式化另一种方式print(aaa.format_map({'city':'石家庄','name':"行唐"}))    #字符串的格式化,中间传的是字典,这种不常用print(information.isdigit())    #判断字符串是不是数字print(information.lower())    #小写print(information.upper())    #大写print(information.replace('this','This',1))    #替换,后的1表示,替换的次数print(''.join([information,information1]))    #字符串拼接,中间用列表,元组,字典的形式传都可以

用python写九九乘法表(仅作为一种娱乐)

python2.7里面的代码

#!/usr/bin/env pythonfor line in range(1,10):    for width in range(1,line+1):        print "%s*%s=%s " %(line,width,line*width),    print

python3.x里面的代码

#!/usr/bin/env pythonfor line in range(1,10):    for width in range(1,line+1):        print("%s*%s=%s " %(line,width,line*width),end="")    print()