Python字符串類型


Python字符串類型

1.創建

s = 'Hello,man!How are you!'

特性

  • 有序

  • 不可變

    不可變如何體現,看代碼:

    a = 'blue'
    print(id(a))  # 輸出結果 34390568
    a = 'pink'
    print(id(a))  # 輸出結果 41910600
    

    當給a二次復制的時候,並未更改 'blue',而是重新開辟了內存空間存放 'pink'

2.字符串方法

如何進入到源碼查看,隨便寫一個字符串方法,Ctrl + 鼠標左擊,然后找到str 的方法,如下圖

這是C語言寫的,所以我們並不會看到python的真正源碼是如何實現的,只能看到解釋

01 capitalize() 首字母大寫,其他字母小寫
>>> a = 'Hello world'
>>> a.capitalize()
'Hello world'
02 casefold() 全部轉化為小寫

對於字符串比較的時候,可以全部轉化為小寫然后比較

>>> a = 'Hello world'
>>> a.casefold()
'hello world'
03 center() Return S centered in a string of length width. Padding isdone using the specified fill character (default is a space)
>>> a
'Hello world'
>>> a.center(50,'*')
'*******************Hello world********************'
04 count() 計算包含多少個某字符
>>> a = 'Hello world'
>>> a.count('o')
2
>>> a.count('l',0,3) #從0到3,包含多少個 l, 區間[H,e,l,l)
1
05 endswith() 判斷是不是以 什么 結尾,返回Boolean
>>> a = 'Hello world!'
>>> a.endswith('!')
True
>>> a.endswith('d!')
True
>>> a.endswith('d')
False
06 expandtabs() 增加tabs長度的
>>> a = 'a\tb' #\t表示一個tab
>>> print(a)
a       b
>>> a.expandtabs(20)
'a                   b'
07 find() 查找,找到返回index,找不到返回-1
>>> a = 'Hello world!'
>>> a.find('o') #從左到右,返回找到的first 目標原素就返回
4
>>> a.find('k')
-1
>>> a.find('l',0,4) 
2
08 format() 返回一個新字符串,不改變原字符串
'I am {0},and I like {1}'
>>> str.format('gudon','apple')
'I am gudon,and I like apple'
>>> print(str)
I am {0},and I like {1}
>>> str = 'I am {name},and I like {fruts}'
>>> str.format(name='jack',fruts='orange')
'I am jack,and I like orange'
09 isdecimal() 法檢查字符串是否只包含十進制字符 ,這種方法只存在於unicode對象。

注意:定義一個十進制字符串,只需要在字符串前添加 'u' 前綴即可。

>>> str = u'this2018'
>>> str.isdecimal()
False
>>> str = u'2018'
>>> str.isdecimal()
True
10 isdigit() 方法檢測字符串是否只由數字組成。
>>> '2018'.isdigit()
True
>>> 'hello'.isdigit()
False
11 isidentifier()判斷是否為Python中的標識符
>>> '33a'.isidentifier()
False
>>> '_name'.isidentifier()
True
12 islower() 判斷是否都是小寫 ,相對與 isupper()
#islower()
>>> '33abc'.islower()
True
>>> '33abDc'.islower()
False
# isupper()
>>> 'ASD'.isupper()
True
>>> 'ASDaa'.isupper()
False
13 isnumeric() 如果字符串中只包含數字字符,則返回 True,否則返回 False
str = u"this2009";  
print str.isnumeric(); #False

str = u"23443434";
print str.isnumeric(); #True

#對於 Unicode 數字、全角數字(雙字節)、羅馬數字和漢字數字會返回 True ,其他會返回 False。byte數字(單字節)無此方法。
print u'123'.isnumeric() # True
print u'Ⅷ'.isnumeric() # True
print u'abc123'.isnumeric() # False
print u'1.23'.isnumeric() # False
>>> '五'.isnumeric() #True

num = "1"  #unicode
num.isdigit()   # True
num.isdecimal() # True
num.isnumeric() # True

num = "1" # 全角
num.isdigit()   # True
num.isdecimal() # True
num.isnumeric() # True

num = b"1" # byte
num.isdigit()   # True
num.isdecimal() # AttributeError 'bytes' object has no attribute 'isdecimal'
num.isnumeric() # AttributeError 'bytes' object has no attribute 'isnumeric'

num = "IV" # 羅馬數字
num.isdigit()   # True
num.isdecimal() # False
num.isnumeric() # True

num = "四" # 漢字
num.isdigit()   # False
num.isdecimal() # False
num.isnumeric() # True

14 join()
>()>> names = ['gudon','jack','fesco']
>>> names
['gudon', 'jack', 'fesco']
>>> ''.join(names)
'gudonjackfesco'
>>> '+'.join(names)
'gudon+jack+fesco'
15 lower() and upper() 轉換大小寫
>>> s.lower()
'hello world'
>>> s.upper()
'HELLO WORLD'
16 Python ljust() 方法返回一個原字符串左對齊,並使用空格填充至指定長度的新字符串。如果指定的長度小於原字符串的長度則返回原字符串。
>>> s = 'Hello world'
>>> s.ljust(50)
'Hello world                                       '
>>> s.ljust(50,'*')
'Hello world***************************************'
>>> s.ljust(5)
'Hello world'
17 strip() 去除空格等 ,lstrip()只去除左邊 rstrip()只去除右邊
>>> s = '\n hello world     '
>>> s
'\n hello world     '
>>> s.strip()
'hello world'
18 replace() 字符串替換
>>> s = 'Hello world'
>>> s.replace('l','L')
'HeLLo worLd'
>>> s.replace('l','L',2)
'HeLLo world'
19 partition() 根據傳入的指定分隔符,返回一個3元的元祖,第一個為左邊字符串,第二個為分隔符本身,第三個為分隔符右邊字符串,對應的有 rpartition(),從右邊開始分
>>> s = 'www.fesco.com.cn'
>>> s.partition('.')
('www', '.', 'fesco.com.cn')
>>> s.rpartition('.')
('www.fesco.com', '.', 'cn')
20 split() 通過指定分隔符對字符串進行切片,如果參數num 有指定值,則僅分隔 num 個子字符串 ,從右邊開始分,rsplit()
>>> str = 'this is an apple'
>>> str.split() # 分隔符,默認為所有的空字符,包括空格、換行(\n)、制表符(\t)等
['this', 'is', 'an', 'apple']
>>> str.split('a')
['this is ', 'n ', 'pple']
>>> str.split('i',1)
['th', 's is an apple']
21 startswith() and endswith()
>>> str = 'Hello world!'
>>> str.startswith('H')
True
>>> str.endswith('d!')
True
22 swapcase() 大小寫切換,原來是大寫,變成小寫,原來是小寫的,變成大寫
>>> str = 'Hello world!'
>>> str.swapcase()
'hELLO WORLD!'


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM