码迷,mamicode.com
首页 > 编程语言 > 详细

【我们一起自学Python】-字符串操作

时间:2017-08-16 15:24:07      阅读:165      评论:0      收藏:0      [点我收藏+]

标签:python初学者

这里列举一下字符串的一些操作,方便以后用到时查询使用。

1.capitalize
print("my name is Galen".capitalize())#首字母大写

My name is galen

2.center

print("My name is Galen".center(30,"-"))#字符串居中显示30个字节,其他部分用"-"横线填充
print("My name is Galen".ljust(30,"-"))#字符串居左显示30个字节,其他部分用"-"横线填充
print("My name is Galen".rjust(30,"-"))#字符串居右显示30个字节,其他部分用"-"横线填充

-------My name is Galen-------
My name is Galen--------------
--------------My name is Galen

3.endswith、startswith

print("My name is Galen".endswith("my"))#判断字符串是否一my结尾,如果是返回True,不是则返回False
print("My name is Galen".startswith("My"))#判断字符串是否一my开始,如果是返回True,不是则返回False
False
True

4.find rfind

print("my name is galen".find("na"))#如果子字符在母字符串中有多个,则最右边一个子字符串首字符的索引
print("my name is gnalen".rfind("na"))#如果子字符在母字符串中有多个,则最右边一个子字符串首字符的索引
3
12
#使用find和切片配合使用可以删除字符串中的某些字符
str1 = "My name is Galen"
find = "me"
if find in  str1:
    n = str1.find(find)
    l = len(str1)
    str1 = str1[:n]+str1[n+len(find):]
    print(str1)
My na is Galen

5.count

print("My name is Galen".count("m"))#计算出字符在该字符串中出现的次数
1

6.encode,decode

to_byte = "My name is Galen,我爱祖国".encode(encoding="GBK")#将字符串从编码成二进制形式,encoding指定原字符串的编码形式
print(to_byte)
to_str = to_byte.decode(encoding="GBK")#将二进制解编码成字符串,这里的encoding是转换成二进制是的编码,不然会报错
print(to_str)
b‘My name is Galen\xa3\xac\xce\xd2\xb0\xae\xd7\xe6\xb9\xfa‘
My name is Galen,我爱祖国

7.format,format_map

str2 = "my name is {name},i am {age} years old"
print(str2)
print(str2.format(name="Galen",age="21"))#字符串格式化
print(str2.format_map(  {"name":"Galen","age":"21"}  ))#字符串格式化,可以引入字典中的元素
my name is {name},i am {age} years old
my name is Galen,i am 21 years old
my name is Galen,i am 21 years old

8.index

print("My name is Galen".index("na"))#返回字符串"na"首字符在字符串中的索引值,如果存在多个,则返回首字符在左边数第一个的索引
print("My name is Galena".rindex("na"))#返回字符串"na"首字符在字符串中的索引值,如果存在多个,则返回首字符在右边数第一个的索引
3
15

9.isalnum

print("12as".isalnum())#如果该字符串中只有阿拉伯数字和字符则返回True,如果包含特殊字符则返回False
print("12as@".isalnum())#如果该字符串中只有阿拉伯数字和字符则返回True,如果包含特殊字符则返回False

10.isalpha

print("asfCd".isalpha())#如果字符串中只含有英文字母(包括大写)则返回True,否则返回False
print("asfd1".isalpha())

11.isdigit

print("123".isdigit())#如果该字符串是否整数则返回True,否则返回False
print("123a".isdigit())#
print("123.1".isdigit())#

12.isidentifier

print("_123a".isidentifier())#判断是不是一个合法的标识符(变量名),如果可以作为变量名则返回True,否则返回False
print("123.1".isidentifier())#判断是不是一个合法的标识符(变量名)

13.islower

print("str1".islower())#如果字符串都是小写则返回True,反则返回False
print("Str1".islower())#

14.isnumeric

print("12.12".isnumeric())#判断字符串中是否只有数字,如果只有数字则返回True,否则返回False
print("1212".isnumeric())

15.isspace

print("12 12".isspace())#判断该字符是否是个空格,是则返回True,不是则返回False
print(" ".isspace())#判断该字符是否是个空格,是则返回True,不是则返回False

16.title

print("my name is Galen".istitle())#判断该字符串是否是标题(即每个单词的首字符大写),如果是则返回True,否则返回False
print("My Name s Galen".istitle())
print("my name is Galen".title())#将该字符串转换成标题,即将每个单词的首字母转换成大写

17.isprintable

print("MY NAME".isprintable())#判断该字符串是否能打印,能打印返回True,不能打印返回False,在Linux中device文件等不能打印则返回False

18.isupper

print("My NAME".isupper())#判断该字符串是否全是大写,全是大写则返回True,否则返回False
print("MY NAME".isupper())

19.join

print("_".join("123"))#将前一个字符串插入后面的字符串中。
print("_".join(["Galen","Jack","Mark"]))#将前一个字符串插入后面的字符串中。

20.lower,upper

print("Galen".lower())#将字符串中的大写变成小写
print("Galen".upper())#将字符串中的小写变成大写

21.strip

print(" Galen".lstrip())#去掉字符左端的换行和回车
print(" \nGalen".lstrip())#去掉字符左端的换行和回车
print(" Galen ".strip())#去掉字符两端的换行和回车
print("Galen \n".rstrip())#去掉字符右端的换行和回车
print("----")

22.translate,maketrans

str_mak = str.maketrans("abcdefghijkG","1234567890!@")#前面的字符串和后面的字符串一一对应
print("Galen".translate(str_mak))#将字符串按照str_mak中两个字符串的对应关系替换

23.replace

print("my name is galen".replace("n","N"))#将一个字符串换层另外一个字符串,默认情况下将所有的旧的的字符串都换成新的字符串
print("my name is galen".replace("n","N",1))#可以限制替换的个数,从前到后按顺序替换
print("my name is galen".replace("na","NA"))

24.split,splitlines

print("my name is galen".split())#将字符串分割,并保存在一个列表中,默认是以空格进行分割
print("my_name_is_galen".split("_"))#这次以下划线进行分割
print("my_name_is_galen".split("_",1))#这次以下划线进行分割,但是只进行1次,从左边开始分割的
print("my_\nname_is\n_galen".splitlines())#按着换行符进行分割

25.swapcase

print("My Name Is Galen".swapcase())#将大写字母换成小写,小写字母换成大写

26.zfill

print("My Name Is Galen".zfill(50))#在原字符串的左边用 0 填充,直到整个字符长度为50个字符


本文出自 “纯醪八盅VSIT” 博客,请务必保留此出处http://chunlaobazhong.blog.51cto.com/11499723/1956611

【我们一起自学Python】-字符串操作

标签:python初学者

原文地址:http://chunlaobazhong.blog.51cto.com/11499723/1956611

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!