字符串str常見的函數使用

 

字符串:a='hello python'python

1)大寫 upper()git

print(a.upper())api

返回:HELLO PYTHONspa


字符串:a='HELLO PYTHON'索引

2)小寫 lower()字符串

print(a.lower())it

返回:hello pythondi


字符串:a='Hello pythoN'co

3)大小寫互換 swapcase()
print(a.swapcase())join

返回:hELLO PYTHOn

 


字符串:a='Hello pythoN'
4)查找元素 find()

print(a.find('p'))

返回:6 備註:p元素的索引值

print(a.find('W'))

返回:-1,備註:表示查找不到該元素

 

字符串:a='Hello pythoN'

5)切片 : 起始索引值:結束索引值:步長
a=a[0:11:2]

print(a)

返回:Hlopto

 


字符串:a='Hello pythoN'

6)鏈接 join()

print('@'.join(a))

返回:H@e@l@l@o@ @p@y@t@h@o@N

 


字符串:a='Hello pythoN'

7)分割 split()

print(a.split(' '))

返回:['Hello', 'pythoN'] 列表

 

 

字符串:a='hello python'

8)首字母大寫 capitalize()

print(a.capitalize())

返回:Hello python

 

字符串:a='hello python'

9)某個元素的個數 count()

print(a.count('h'))

返回:2 元素的個數

 

 

字符串:a='hello python'

10)查看元素的長度 len()

print(len(a))
返回:12 一共有12個元素


字符串:a='12'
11)判斷元素是不是數字,大寫,小寫

print(a.isdigit())

print(a.islower())
print(a.isupper())

返回:True
False
False

相關文章
相關標籤/搜索