日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

Python 基础数据类型 -字符串(str)的详细用法

發布時間:2025/3/20 python 11 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Python 基础数据类型 -字符串(str)的详细用法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

字符串是編程中最重要的數據類型,也是最常見的

1.字符串的表示方式

-單引號' '

  • 雙引號 " "
  • 多引號 """ """" 、 ''' '''
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"

2.多行字符串

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

''' 學習中遇到問題沒人解答?小編創建了一個Python學習交流QQ群:725638078 尋找有志同道合的小伙伴,互幫互助,群里還有不錯的視頻學習教程和PDF電子書! ''' # 多行字符串 print(""" hello world """) print(""" this is my name poloyy """)# 輸出結果 hello worldthis is my name poloyy

3.轉義符

在字符前加 \ 就行

常見的有

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

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

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

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

''' 學習中遇到問題沒人解答?小編創建了一個Python學習交流QQ群:725638078 尋找有志同道合的小伙伴,互幫互助,群里還有不錯的視頻學習教程和PDF電子書! ''' print("反斜杠 \\ 是什么") print("換行符是什么 \\n")# 輸出結果 反斜杠 \ 是什么 換行符是什么 \n

window 路徑的栗子

print("c:\python\type") print("c:\\python\\type")# 輸出結果 c:\python\ c: type c:\python\type

更簡潔的解決方法

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

print(r"c:\python\type")# 輸出結果 c:\python\type

4.字符串運算:+ 運算

直接拼接的作用

# + 運算 print("123" + "123") print("123" + "abc") print("123", "456")# 輸出結果 123123 123abc 123 456

5.字符串運算:下標和切片

**
**

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

''' 學習中遇到問題沒人解答?小編創建了一個Python學習交流QQ群:725638078 尋找有志同道合的小伙伴,互幫互助,群里還有不錯的視頻學習教程和PDF電子書! ''' # 獲取字符串某個字符 str = "hello world" print(str[0]) print(str[1]) print(str[6]) print(str[-1]) print(str[-5])# 輸出結果 h e w d l

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

獲取字符串中一段字符

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

str[start : end : step]
  • 獲取列表 列表 中在 [start, end) 范圍的子字符串
  • start:閉區間,包含該下標的字符,第一個字符是 0
  • end:開區間,不包含該下標的字符
  • step:步長,設為 n,則每隔 n 個元素獲取一次
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:]) # 取倒數第 5 個字符到最后一個字符print("hello world'[0:5] ", 'hello world'[0:5]) # 取第 1 個字符到第 5 個字符 print("hello world'[0:-5] ", 'hello world'[0:-5]) # 取第 1 個字符直到倒數第 6 個字符 print("hello world'[6:10] ", 'hello world'[6:10]) # 取第 7 個字符到第 10 個字符 print("hello world'[6:-1] ", 'hello world'[6:-1]) # 取第 7 個字符到倒數第 2 個字符 print("hello world'[-5:-1] ", 'hello world'[-5:-1]) # 取倒數第 5 個字符到倒數第 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:] worldhello world'[0:5] hello hello world'[0:-5] hello hello world'[6:10] worl hello world'[6:-1] worl hello world'[-5:-1] worlhello world'[::-1] dlrow olleh hello world'[::2] hlowrd hello world'[1:7:2] el

6.獲取字符串長度

print(len("123"))# 輸出結果 3

結尾給大家推薦一個非常好的學習教程,希望對你學習Python有幫助!

Python基礎入門教程推薦

Python爬蟲案例教程推薦

總結

以上是生活随笔為你收集整理的Python 基础数据类型 -字符串(str)的详细用法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。