Python基本數(shù)據(jù)類型之字符串str
字符串的表示方式
- 單引號(hào) ' '
- 雙引號(hào) " "
- 多引號(hào) """ """" 、 ''' '''
print("hello world")
print('hello world')
print("""hello world""")
# 輸出結(jié)果
hello world
hello world
hello world
為什么需要單引號(hào),又需要雙引號(hào)
因?yàn)榭梢栽趩我?hào)中包含雙引號(hào),或者在雙引號(hào)中包含單引號(hào)
# 單雙引號(hào)
print("hello 'poloyy' world")
print('this is my name "poloyy"')
# 輸出結(jié)果
hello 'poloyy' world
this is my name "poloyy"
多行字符串
正常情況下,單引號(hào)和雙引號(hào)的字符串是不支持直接在符號(hào)間換行輸入的,如果有需要可以用多引號(hào)哦!
# 多行字符串
print("""
hello
world
""")
print("""
this
is
my
name
poloyy
""")
# 輸出結(jié)果
hello
world
this
is
my
name
poloyy
轉(zhuǎn)義符
在字符前加 \ 就行
常見的有
- \n:換行
- \t:縮進(jìn)
- \r:回車
栗子
比如在字符串雙引號(hào)間還有一個(gè)雙引號(hào),就需要用轉(zhuǎn)義符
# 轉(zhuǎn)義符
print("hello \"poloyy\" world")
print('my name is \'poloyy\'')
# 輸出結(jié)果
hello "poloyy" world
my name is 'poloyy'
假設(shè) \ 只想當(dāng)普通字符處理呢?
print("反斜杠 \\ 是什么")
print("換行符是什么 \\n")
# 輸出結(jié)果
反斜杠 \ 是什么
換行符是什么 \n
window 路徑的栗子
print("c:\nothing\rtype")
print("c:\\nothing\\rtype")
# 輸出結(jié)果
c:\nothing\
c:
type
c:\nothing\rtype
更簡(jiǎn)潔的解決方法
用轉(zhuǎn)義符會(huì)導(dǎo)致可讀性、維護(hù)性變差,Python 提供了一個(gè)更好的解決方法:在字符串前加r
print(r"c:\nothing\rtype") # 輸出結(jié)果 c:\nothing\rtype
python3的url編碼和解碼,自定義gbk、utf-8的例子 http://www.fzitv.net/article/168181.htm
字符串運(yùn)算:下標(biāo)和切片
獲取字符串中某個(gè)字符
字符串是一個(gè)序列,所以可以通過下標(biāo)來獲取某個(gè)字符
# 獲取字符串某個(gè)字符 str = "hello world" print(str[0]) print(str[1]) print(str[6]) print(str[-1]) print(str[-5]) # 輸出結(jié)果 h e w d l
如果是負(fù)數(shù),那么是倒數(shù),比如 -1 就是倒數(shù)第一個(gè)元素,-5 就是倒數(shù)第五個(gè)元素
獲取字符串中一段字符
Python 中,可以直接通過切片的方式取一段字符
切片的語法格式
str[start : end : step]
- start:閉區(qū)間,包含該下標(biāo)的字符,第一個(gè)字符是 0
- end:開區(qū)間,不包含該下標(biāo)的字符
- step:步長(zhǎng)
栗子
print("hello world'[:] ", 'hello world'[:]) # 取全部字符
print("hello world'[0:] ", 'hello world'[0:]) # 取全部字符
print("hello world'[6:] ", 'hello world'[6:]) # 取第 7 個(gè)字符到最后一個(gè)字符
print("hello world'[-5:] ", 'hello world'[-5:]) # 取倒數(shù)第 5 個(gè)字符到最后一個(gè)字符
print("hello world'[0:5] ", 'hello world'[0:5]) # 取第 1 個(gè)字符到第 5 個(gè)字符
print("hello world'[0:-5] ", 'hello world'[0:-5]) # 取第 1 個(gè)字符直到倒數(shù)第 6 個(gè)字符
print("hello world'[6:10] ", 'hello world'[6:10]) # 取第 7 個(gè)字符到第 10 個(gè)字符
print("hello world'[6:-1] ", 'hello world'[6:-1]) # 取第 7 個(gè)字符到倒數(shù)第 2 個(gè)字符
print("hello world'[-5:-1] ", 'hello world'[-5:-1]) # 取倒數(shù)第 5 個(gè)字符到倒數(shù)第 2 個(gè)字符
print("hello world'[::-1] ", 'hello world'[::-1]) # 倒序取所有字符
print("hello world'[::2] ", 'hello world'[::2]) # 步長(zhǎng)=2,每?jī)蓚€(gè)字符取一次
print("hello world'[1:7:2] ", 'hello world'[1:7:2]) # 步長(zhǎng)=2,取第 2 個(gè)字符到第 7 個(gè)字符,每?jī)蓚€(gè)字符取一次
# 輸出結(jié)果
hello world'[:] hello world
hello world'[0:] hello world
hello world'[6:] world
hello world'[-5:] world
hello world'[0:5] hello
hello world'[0:-5] hello
hello world'[6:10] worl
hello world'[6:-1] worl
hello world'[-5:-1] worl
hello world'[::-1] dlrow olleh
hello world'[::2] hlowrd
hello world'[1:7:2] el
字符串的函數(shù)
Python 提供了很多內(nèi)置的字符串函數(shù),具體可看
http://www.fzitv.net/article/169790.htm
到此這篇關(guān)于Python - 基本數(shù)據(jù)類型_str 字符串的文章就介紹到這了,更多相關(guān)Python字符串str內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python中常用信號(hào)signal類型實(shí)例
這篇文章主要介紹了Python中常用信號(hào)signal類型實(shí)例,分享了相關(guān)代碼示例,小編覺得還是挺不錯(cuò)的,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-01-01
Python?中?key?參數(shù)的含義及用法小結(jié)
我們?cè)谑褂?sorted()?或?map()?函數(shù)的時(shí)候,都會(huì)看到里面有一個(gè)?key?參數(shù),其實(shí)這個(gè)?key?參數(shù)也存在于其他內(nèi)置函數(shù)中(例如?min()、max()?等),那么我們今天就來了解一下?key?參數(shù)的含義以及用途吧,需要的朋友可以參考下2023-12-12
詳解pandas中iloc, loc和ix的區(qū)別和聯(lián)系
這篇文章主要介紹了詳解pandas中iloc, loc和ix的區(qū)別和聯(lián)系,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03
Python自動(dòng)化辦公之群發(fā)郵件案例詳解
我們?cè)谵k公時(shí)常常會(huì)遇到需要將郵件群發(fā)給很多客戶,這個(gè)時(shí)候如何快速完成這一任務(wù)呢?不要慌,本文將為大家提供用Python代碼解決這一問題的方法,需要的可以參考一下2022-02-02
Python使用pycharm實(shí)現(xiàn)無限彈窗程序
這篇文章主要為大家詳細(xì)介紹了Python如何,pycharm實(shí)現(xiàn)無限彈窗程序,當(dāng)然這一程序非病毒程序,僅整蠱使用,感興趣的小伙伴可以了解一下2024-01-01
對(duì)Python3中列表乘以某一個(gè)數(shù)的示例詳解
今天小編就為大家分享一篇對(duì)Python3中列表乘以某一個(gè)數(shù)的示例詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-07-07

