python基础之字符串篇

字符串是 Python 中最常用的数据类型。我们可以使用单引号或者双引号来创建字符串。

例如: 

str_example = "demo"

str_example = 'demo'

字符串常用的方法

1. find() 方法
   语法:  str.find(substr)
  例子:
  str_example = "You got a dream, you gotta protect it. People can't do something themselves,they wanna tell you , you can't do it.If you want something, go get it. "
  #查找dream在str_example第一次出现的位置
  position = str_example.find('dream')  # 10
  #可以指定位置查询
  position = str_example.find('you',10) # 17
  position = str_example.find('you',20,100) # 92
  #可以指定位置查询,如果查询不到,则返回-1
  position = str_example.find('you',20,50) # -1
2. rfind() 方法 和find() 方法用法一样,只不过是从右向左查找
   语法: str.rfind(substr)
  例子:
  #查找dream在mystr从右向左指定区域内第一次出现的位置
  mystr = "You got a dream, you gotta protect it. People can't do something themselves,they wanna tell you you can't do it.If you want something, go get it. "
  flag
= mystr.rfind('you',20,120) #115
3. index() 方法 ,在字符串中查找子字符串出现的位置
   语法 str.index(substr)
  例子:   
#查找第一次出现的位置,如果没有则会跑出ValueError异常 mystr = "You got a dream, you gotta protect it. People can't do something themselves,they wanna tell you you can't do it.If you want something, go get it. " mystr.index('you') # 115 mystr.index('she') # ValueError: substring not found 所以避免异常发生 try: mystr.index('she') except Exception as e: print(-1)
4. rindex()方法 和index()方法用法一致,只不过是在右侧向左侧查找
    语法:
    str.rindex(substr)
    例子:
     mystr = "You got a dream, you gotta protect it. People can't do something themselves,they wanna tell you you can't do it.If you want something, go get it. "
   mystr.rindex('you') #115
    mystr.rindex('she') # ValueError: substring not found
   #查找从右到左第一次出现的位置,如果没有则会跑出ValueError异常
    try:
        mystr.rindex('she')
    except Exception as e:
        print(-1
5. count() 方法 查询子字符串出现的次数
    语法:str.count(substr)
    例子:
    #查找在mystr中you出现的次数 mystr
= "You got a dream, you gotta protect it. People can't do something themselves,they wanna tell you you can't do it.If you want something, go get it. " mystr.count('you') # 4
6. len() 方法 计算字符串的长度
    语法:len(str)
    例子:
    #计算 mystr 的长度
        mystr = "You got a dream, you gotta protect it. People can't do something themselves,they wanna tell you you can't do it.If you want something, go get it. "
     len(mystr)# 146
7. replace()方法,字符串替换
    语法 str.replace('new_str','old_str')
    例子:
    # 将mystr中的 you 替换成 你
    mystr = "You got a dream, you gotta protect it. People can't do something themselves,they wanna tell you you can't do it.If you want something, go get it. "
    mystr.replace('you','')# You got a dream, 你 gotta protect it. People can't do something themselves,they wanna tell 你 你 can't do it.If 你 want something, go get it. 
   # 指定修改个数
   mystr = "You got a dream, you gotta protect it. People can't do something themselves,they wanna tell you you can't do it.If you want something, go get it. "
   mystr.replace('you','你',2) # You got a dream, 你 gotta protect it. People can't do something themselves,they wanna tell 你 you can't do it.If you want something, go get it
8. split()方法  字符串分割方法
    语法:
    str.split(substr)
    例子:
    #以空格 分隔字符串 获取一个list列表
    mystr = "You got a dream, you gotta protect it. People can't do something themselves,they wanna tell you you can't do it.If you want something, go get it. "
    mystr.split(' ') # ['You', 'got', 'a', 'dream,', 'you', 'gotta', 'protect', 'it.', 'People', "can't", 'do', 'something', 'themselves,they', 'wanna', 'tell', 'you', 'you', "can't", 'do', 'it.If', 'you', 'want', 'something,', 'go', 'get', 'it.', '']
9. capitalize() 方法  首字母大写
    语法:
    str.capitalize() 
    例子:
    mystr = "You got a dream, you gotta protect it. People can't do something themselves,they wanna tell you you can't do it.If you want something, go get it. "
    mystr.capitalize() #You got a dream, you gotta protect it. people can't do something themselves,they wanna tell you you can't do it.if you want something, go get it. 
10. titile() 方法,字符串标题化,也就是说字符串每个单词的首字母大写,其余为小写
    语法: str.title()
    例子:
    mystr = "You got a dream, you gotta protect it. People can't do something themselves,they wanna tell you you can't do it.If you want something, go get it. "
    mystr.tyitle() # You Got A Dream, You Gotta Protect It. People Can'T Do Something Themselves,They Wanna Tell You You Can'T Do It.If You Want Something, Go Get It. 
11. istitle()方法 判断 字符串每个单词的首字母是否大写,其余为小写,返回boolean值
    语法: str.istitle()
    例子:
    mystr = "You got a dream, you gotta protect it. People can't do something themselves,they wanna tell you you can't do it.If you want something, go get it. "
    mystr.istitle() #Flase
    mystr1 = 'Hello World'
    mystr1.istitle() # True
12. startswith() 判断字符串是否以某子字符串开头,返回boolean值
    语法: str.startswith(substr)
    例子:
    mystr = "You got a dream, you gotta protect it. People can't do something themselves,they wanna tell you you can't do it.If you want something, go get it. "
    mystr.startswith('Y') # True
    mystr.startswith('x') #Flase
13. endswith() 判断字符串是否以某子字符串结尾,返回boolean值
    语法:str.endswith(substr)
    例子:
    mystr = "You got a dream, you gotta protect it. People can't do something themselves,they wanna tell you you can't do it.If you want something, go get it. "
    mystr.endswith(' ') # True
    mystr.endswith('.') # Flas
14. lower() 方法 字符串所有字符转化成小写字母
    语法: str.lower()
    例子:
    mystr = "You got a dream, you gotta protect it. People can't do something themselves,they wanna tell you you can't do it.If you want something, go get it. "
    mystr.lower() # you got a dream, you gotta protect it. people can't do something themselves,they wanna tell you you can't do it.if you want something, go get it.
15. upper() 方法 字符串所有字符转换成大写字母
    语法:str.upper()
    例子:
    mystr = "You got a dream, you gotta protect it. People can't do something themselves,they wanna tell you you can't do it.If you want something, go get it. "
    mystr.upper() # YOU GOT A DREAM, YOU GOTTA PROTECT IT. PEOPLE CAN'T DO SOMETHING THEMSELVES,THEY WANNA TELL YOU YOU CAN'T DO IT.IF YOU WANT SOMETHING, GO GET IT.
16. ljust() 方法 返回一个原字符串左对齐,并使用空格填充至指定长度的新字符串。如果指定的长度小于原字符串的长度则返回原字符串。
    语法: str.ljust(width,fillchar)
    例子:
    mystr = "You got a dream"
    mystr.ljust(50,'*') # You got a dream***********************************
17. rjust()方法 返回一个字符串右对齐,并使用空格或指定字符填充新的字符串,如果指定的长度小于原字符串,则返回原字符串
    语法: str.rjust(width,fiilchar)
    例子:
    mystr = 'You got a dream'
    mystr.rjust(50,'*') # ***********************************You got a dream
18. center() 方法 返回一个原字符串居中,并使用空格填充至长度 width 的新字符串。默认填充字符为空格。
    语法: str.center(width,fillchar)
    例子:
    mystr = "You got a dream"
    mystr.center(50,'*') # *****************You got a dream******************
19. lstrip()方法  去除左边的空格、换行符或者指定字符
    语法: str.lstrip()
    例子:
    mystr = ' You got a dream'
    mystr.lstrip() #You got a dream
    mystr.lstrip(' Y') #ou got a dream
20. rstrip()方法  去除右边的空格、换行符或者指定字符
    语法: str.rstrip()
    例子:
    mystr = ' You got a dream '
    mystr.rstrip() # You got a dream
    mystr.lstrip(' m') # You got a drea
21. strip()方法  去除两侧的空格、换行符或者指定字符
    语法: str.strip()
    例子:
    mystr = ' You got a dream '
    mystr.strip() #You got a dream
22. partition() 方法 用来根据指定的分隔符将字符串进行分割,返回一个元组
    语法:str.partition(substr)
    例子:
    mystr = 'You got a dream'
    mystr.partition('a') # (' You got ', 'a', ' dream')
23. rpartition()方法 从右向左找指定字符分割成元祖
    语法: str.rpartition(substr)
    例子:
    mystr = 'You got a dream'
    mystr.rpartition('a') # (' You got a dre', 'a', 'm')
24. splitlines()方法 按照行('\r', '\r\n', \n')分隔,返回一个包含各行作为元素的列表,如果参数 flag为 False,不包含换行符,如果为 True,则保留换行符。
    语法:str.splitlines(flag)
    例子:
    mystr = 'You got a\n dream'
    mystr.splitlines() # [' You got a', ' dream']
    mystr.splitlines(True) # [' You got a\n', ' dream']
25. isalnum()方法 判断字符串是否只由数字和字母组成,返回boolean值
    语法:str.isalnum()
    例子:
    mystr = 'Yougot3dreams'
    mystr.isalnum() # True
    mystr1 = 'You got 3 dreams !'
    mystr1.isalnum() # False
26. isdigit() 方法 判断字符串是否只有数字 返回boolean值
    语法: str.isdigit()
    例子:
    mystr = '0123456789'
    mystr.isdigit() # True
    mystr1 = '0123456789!'
    mystr1.isdigit() # Flase
27. isalpha() 方法 判断字符串是否只有字母组成,返回boolean值
    语法:str.isalpha()
    例子:
    mystr = 'Yougotadream'
    mystr.isalpha() #True
    mystr1 = 'You got a dream'
    mystr1.isalpha() # Flase
28. isspace()方法 判断字符串是否只由空格组成,返回boolean值
    语法: str.isspace() 
    例子:
    mystr = ' '
    mystr.isspace() #True
    mystr1 = 'You got a dream'
    mystr1.isspace() # False
29. join() 方法 用于将序列中的元素以指定的字符连接生成一个新的字符串。    
    语法: str.join(str)
    例子:
    mystr1 = ' '
    mystr2 = 'hello'
    mystr1.join(mystr2) # h e l l o
原文地址:https://www.cnblogs.com/wuxiaoshi/p/9726895.html