文章末尾獲取思惟導圖高清源文件python
使用「+」能夠對多個字符串進行拼接
語法格式:str1 + str2
shell
>>> str1 = "aaa" >>> str2 = "bbb" >>> print(str1 + str2) aaabbb
須要注意的是字符串不容許直接與其餘類型進行拼接,例如函數
>>> num = 100 >>> str1 = "hello" >>> print(str1 + num) Traceback (most recent call last): File "<pyshell#5>", line 1, in <module> print(str1 + num) TypeError: can only concatenate str (not "int") to str
上面這種狀況咱們能夠將num轉換爲字符串再進行拼接編碼
>>> num = 100 >>> str1 = "hello" >>> print(str1 + str(num)) hello100
這樣就不會報錯了spa
在Python中使用len()函數來計算字符串的長度
語法格式:len(string)
code
>>> str1 = "hello" >>> len(str1) 5 >>> str2 = "你好" >>> len(str2) 2 >>> str3 = "1111" >>> len(str3) 4
從上面的結果咱們能夠看出,在默認狀況下,len函數在計算字符串的長度時,不管是數字,字母仍是多字節的漢字都認爲是一個字符。
爲何說是默認狀況下呢,由於在實際開發中,可能由於咱們採起的編碼不一樣,字符串實際所佔的字節數也不一樣。blog
這時咱們能夠經過使用encode()方法進行編碼後再進行獲取長度。
例如:索引
>>> str1 = "你好" >>> len(str1) 2 >>> len(str1.encode('gbk')) 4 >>> len(str1.encode('utf-8')) 6
語法格式:string[start : end : step]
參數說明ip
>>> str1 = "hello world!" >>> str1[1] #截取第2個字符 'e' >>> str1[2:] #從第3個字符開始截取 'llo world!' >>> str1[:4] 'hell' >>> str1[1:5] 'ello' >>> str1[-1] #截取最後一個字符 '!' >>> str1[2:-2] 'llo worl'
注意:字符串的索引是從0開始的utf-8
python中分割字符串是使用split()方法把字符串分割成列表
語法格式: str.split(sep, maxsplit)
參數說明:
>>> str1 = "i am a good boy!" >>> str1.split() #採用默認分割符進行分割 ['i', 'am', 'a', 'good', 'boy!'] >>> str1.split(" ") #採用空格進行分割 ['i', 'am', 'a', 'good', 'boy!'] >>> str1.split(" ", 3) #採用空格進行分割,而且只分割前3個 ['i', 'am', 'a', 'good boy!']
注意默認狀況下按空格分割
python中字符串的查找方法
一、count()方法
語法格式:str.count(sub[, start[, end]])
做用:用於檢索指定字符串在另外一個字符串中出現的次數,若是檢索的字符串不存在則返回0,不然返回出現的次數。
參數說明
>>> str1 = "hello world" >>> print(str1.count('o')) 2
二、find()方法
語法格式:str.find(sub[, start[, end]])
做用:檢索是否包含指定的字符串,若是檢索的字符串不存在則返回-1,不然返回首次出現該字符串時的索引。
>>> str1 = "hello world!" >>> str1.find('wo') 6
三、index()方法
語法格式:str.index(sub[, start[, end]])
做用:和find方法相似,也用於檢索是否包含指定的字符串,使用index方法,當指定的字符串不存在時會拋異常。
>>> str1 = "hello world!" >>> str1.index('w') 6 >>> str1.index('m') Traceback (most recent call last): File "<pyshell#6>", line 1, in <module> str1.index('m') ValueError: substring not found >>> str1.find('m') -1
四、startswith()方法
語法格式:str.startswith(prefix[, start[, end]])
做用:檢索字符串是否以指定的字符串開頭,若是是則返回true,不然返回false。
>>> str1 = "hello world!" >>> str1.startswith('hello') True >>> str1.startswith('hi') False >>>
五、endswith()方法
語法格式:str.endswith(prefix[, start[, end]])
做用:檢索字符串是否以指定的字符串結尾,若是是則返回true,不然返回false。
>>> str1 = "hello world!" >>> str1.endswith('world!') True >>> str1.endswith('haha') False
一、lower()方法
語法格式:str.lower()
做用:將字符串中的大寫字母轉換爲小寫字母
>>> str1 = "Hello World!" >>> str1.lower() 'hello world!'
二、upper()方法
語法格式:str.upper()
做用:將字符串中的小寫字母轉換爲大寫字母
>>> str1 = "Hello World!" >>> str1.upper() 'HELLO WORLD!'
開發中,咱們會遇到這樣的需求,字符串先後(左右側)不容許出現空格和特殊字符或者將用戶輸入的字符串中誤輸入的空格去除掉。這時咱們就須要用到strip函數。
一、strip()方法
語法格式:str.strip([chars])
做用:去除字符串先後(左右側)的空格或特殊字符
>>> str1 = " hello world! " >>> str1.strip() 'hello world!' >>> str2 = "#hello world#@#" >>> str2.strip('#') 'hello world#@' >>> str3 = "@hello world!@." >>> str3.strip('@.') 'hello world!'
二、lstrip()方法
語法格式:str.lstrip([chars])
做用:去除字符串前面(左側)的空格或特殊字符
>>> str1 = " hello world! " >>> str1.lstrip() 'hello world! ' >>> str2 = "#hello world#@#" >>> str2.lstrip('#') 'hello world#@#' >>> str3 = "@.hello world!@." >>> str3.lstrip('@.') 'hello world!@.'
三、rstrip()方法
語法格式:str.rstrip([chars])
做用:去除字符串後面(右側)的空格或特殊字符
>>> str1 = " hello world! " >>> str1.rstrip() ' hello world!' >>> str2 = "#hello world#@#" >>> str2.rstrip('#') '#hello world#@' >>> str3 = "@.hello world!@." >>> str3.rstrip('@.') '@.hello world!'
所謂格式化字符串就是先制定一個模板,在模板中預留幾個空位,而後根據須要填上相應的內容。
語法格式:'%[-][+][0][.n]格式化字符'%exp
參數說明
>>> template = '學號:%d,姓名:%s,班級:%s' >>> print(template% (123,'張三','一年級')) 學號:123,姓名:張三,班級:一年級
好了,關於Python中字符串的操做方法就介紹到這裏,但願能夠幫助到你們。
須要思惟導圖源文件的請留言你的郵箱。