欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python基本數(shù)據(jù)類型之字符串str

 更新時間:2021年07月19日 09:18:42   作者:小菠蘿測試筆記  
字符串是編程中最重要的數(shù)據(jù)類型,也是最常見的,今天小編抽空給大家講解下Python基本數(shù)據(jù)類型之字符串str的實例代碼,感興趣的朋友跟隨小編一起看看吧

字符串的表示方式

  • 單引號 ' '
  • 雙引號 " "
  • 多引號 """ """"  、 ''' '''
print("hello world")
print('hello world')
print("""hello world""")

# 輸出結果
hello world
hello world
hello world

為什么需要單引號,又需要雙引號

因為可以在單引號中包含雙引號,或者在雙引號中包含單引號

# 單雙引號
print("hello 'poloyy' world")
print('this is my name "poloyy"')

# 輸出結果
hello 'poloyy' world
this is my name "poloyy"

多行字符串

正常情況下,單引號和雙引號的字符串是不支持直接在符號間換行輸入的,如果有需要可以用多引號哦!

# 多行字符串
print("""
hello
world
""")
print("""
this
is
my
name
poloyy
""")

# 輸出結果
hello
world

this
is
my
name
poloyy

轉義符

在字符前加 \ 就行

常見的有

  • \n:換行
  • \t:縮進
  • \r:回車

栗子

比如在字符串雙引號間還有一個雙引號,就需要用轉義符

# 轉義符
print("hello \"poloyy\" world")
print('my name is \'poloyy\'')

# 輸出結果
hello "poloyy" world
my name is 'poloyy'

假設 \ 只想當普通字符處理呢?

print("反斜杠 \\ 是什么")
print("換行符是什么 \\n")

# 輸出結果
反斜杠 \ 是什么
換行符是什么 \n

window 路徑的栗子

print("c:\nothing\rtype")
print("c:\\nothing\\rtype")

# 輸出結果
c:\nothing\
c:
type
c:\nothing\rtype

更簡潔的解決方法

用轉義符會導致可讀性、維護性變差,Python 提供了一個更好的解決方法:在字符串前加r

print(r"c:\nothing\rtype")

# 輸出結果
c:\nothing\rtype

python3的url編碼和解碼,自定義gbk、utf-8的例子 http://www.dbjr.com.cn/article/168181.htm

字符串運算:下標和切片

獲取字符串中某個字符

字符串是一個序列,所以可以通過下標來獲取某個字符

# 獲取字符串某個字符
str = "hello world"
print(str[0])
print(str[1])
print(str[6])
print(str[-1])
print(str[-5])

# 輸出結果
h
e
w
d
l

如果是負數(shù),那么是倒數(shù),比如 -1 就是倒數(shù)第一個元素,-5 就是倒數(shù)第五個元素

獲取字符串中一段字符

Python 中,可以直接通過切片的方式取一段字符

切片的語法格式

str[start : end : step]
  • start:閉區(qū)間,包含該下標的字符,第一個字符是 0
  • end:開區(qū)間,不包含該下標的字符
  • step:步長

栗子

print("hello world'[:] ", 'hello world'[:])  # 取全部字符
print("hello world'[0:] ", 'hello world'[0:])  # 取全部字符
print("hello world'[6:] ", 'hello world'[6:])  # 取第 7 個字符到最后一個字符
print("hello world'[-5:] ", 'hello world'[-5:])  # 取倒數(shù)第 5 個字符到最后一個字符

print("hello world'[0:5] ", 'hello world'[0:5])  # 取第 1 個字符到第 5 個字符
print("hello world'[0:-5] ", 'hello world'[0:-5])  # 取第 1 個字符直到倒數(shù)第 6 個字符
print("hello world'[6:10] ", 'hello world'[6:10])  # 取第 7 個字符到第 10 個字符
print("hello world'[6:-1] ", 'hello world'[6:-1])  # 取第 7 個字符到倒數(shù)第 2 個字符
print("hello world'[-5:-1] ", 'hello world'[-5:-1])  # 取倒數(shù)第 5 個字符到倒數(shù)第 2 個字符

print("hello world'[::-1] ", 'hello world'[::-1])  # 倒序取所有字符
print("hello world'[::2] ", 'hello world'[::2])  # 步長=2,每兩個字符取一次
print("hello world'[1:7:2] ", 'hello world'[1:7:2])  # 步長=2,取第 2 個字符到第 7 個字符,每兩個字符取一次

# 輸出結果
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.dbjr.com.cn/article/169790.htm

到此這篇關于Python - 基本數(shù)據(jù)類型_str 字符串的文章就介紹到這了,更多相關Python字符串str內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • matplotlib常見函數(shù)之plt.rcParams、matshow的使用(坐標軸設置)

    matplotlib常見函數(shù)之plt.rcParams、matshow的使用(坐標軸設置)

    這篇文章主要介紹了matplotlib常見函數(shù)之plt.rcParams、matshow的使用(坐標軸設置),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2021-01-01
  • Python 時間操作例子和時間格式化參數(shù)小結

    Python 時間操作例子和時間格式化參數(shù)小結

    這篇文章主要介紹了Python 時間操作例子,例如取前幾天、后幾天、前一月、后一月等,需要的朋友可以參考下
    2014-04-04
  • python 識別登錄驗證碼圖片功能的實現(xiàn)代碼(完整代碼)

    python 識別登錄驗證碼圖片功能的實現(xiàn)代碼(完整代碼)

    這篇文章主要介紹了python 識別登錄驗證碼圖片功能,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-07-07
  • Python 實現(xiàn)異步調用函數(shù)的示例講解

    Python 實現(xiàn)異步調用函數(shù)的示例講解

    今天小編就為大家分享一篇Python 實現(xiàn)異步調用函數(shù)的示例講解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-10-10
  • python判斷正負數(shù)方式

    python判斷正負數(shù)方式

    這篇文章主要介紹了python判斷正負數(shù)方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-06-06
  • Pandas實現(xiàn)groupby分組統(tǒng)計方法實例

    Pandas實現(xiàn)groupby分組統(tǒng)計方法實例

    在數(shù)據(jù)處理的過程,有可能需要對一堆數(shù)據(jù)分組處理,例如對不同的列進行agg聚合操作(mean,min,max等等),下面這篇文章主要給大家介紹了關于Pandas實現(xiàn)groupby分組統(tǒng)計方法的相關資料,需要的朋友可以參考下
    2023-06-06
  • Python入門之面向對象和類

    Python入門之面向對象和類

    這篇文章主要為大家介紹了Python面向對象和類,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-01-01
  • Windows下安裝Scrapy

    Windows下安裝Scrapy

    今天小編就為大家分享一篇關于Windows下安裝Scrapy,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2018-10-10
  • python源文件的字符編碼知識點詳解

    python源文件的字符編碼知識點詳解

    在本篇文章里小編給大家整理的是一篇關于python源文件的字符編碼知識點詳解,有興趣的朋友們可以學習下。
    2021-03-03
  • Python爬蟲教程知識點總結

    Python爬蟲教程知識點總結

    在本篇文章里小編給大家整理的是一篇關于Python爬蟲教程知識點總結,有興趣的朋友們可以學習參考下。
    2020-10-10

最新評論