佳木斯湛栽影视文化发展公司

主頁 > 知識庫 > Python基本數(shù)據(jù)類型之字符串str

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

熱門標(biāo)簽:銀行業(yè)務(wù) 服務(wù)器配置 美圖手機(jī) 智能手機(jī) 鐵路電話系統(tǒng) 檢查注冊表項(xiàng) 網(wǎng)站文章發(fā)布 呼叫中心市場需求

字符串的表示方式

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

# 輸出結(jié)果
hello world
hello world
hello world

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

因?yàn)榭梢栽趩我栔邪p引號,或者在雙引號中包含單引號

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

# 輸出結(jié)果
hello 'poloyy' world
this is my name "poloyy"

多行字符串

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

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

# 輸出結(jié)果
hello
world

this
is
my
name
poloyy

轉(zhuǎn)義符

在字符前加 \ 就行

常見的有

  • \n:換行
  • \t:縮進(jìn)
  • \r:回車

栗子

比如在字符串雙引號間還有一個(gè)雙引號,就需要用轉(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

更簡潔的解決方法

用轉(zhuǎn)義符會(huì)導(dǎo)致可讀性、維護(hù)性變差,Python 提供了一個(gè)更好的解決方法:在字符串前加r

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

# 輸出結(jié)果
c:\nothing\rtype

python3的url編碼和解碼,自定義gbk、utf-8的例子 https://www.jb51.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:步長

栗子

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])  # 步長=2,每兩個(gè)字符取一次
print("hello world'[1:7:2] ", 'hello world'[1:7:2])  # 步長=2,取第 2 個(gè)字符到第 7 個(gè)字符,每兩個(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ù),具體可看

https://www.jb51.net/article/169790.htm

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

您可能感興趣的文章:
  • python入門字符串拼接\截取\轉(zhuǎn)數(shù)字理解學(xué)習(xí)
  • python入門課程第五講之序列和字符串
  • Python如何使用print()函數(shù)輸出格式化字符串
  • 10個(gè)有用的Python字符串函數(shù)小結(jié)
  • 怎么處理Python分割字符串時(shí)有多個(gè)分隔符
  • Python數(shù)字/字符串補(bǔ)零操作實(shí)例代碼
  • python如何正確的操作字符串
  • python字符串的多行輸出的實(shí)例詳解
  • Python的文本常量與字符串模板之string庫
  • 關(guān)于Python中字符串的各種操作

標(biāo)簽:長治 紅河 上海 樂山 沈陽 河南 滄州 新疆

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Python基本數(shù)據(jù)類型之字符串str》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    寿光市| 名山县| 新密市| 河曲县| 确山县| 延津县| 边坝县| 镇坪县| 瑞丽市| 池州市| 凌海市| 平乡县| 丰顺县| 芦溪县| 苍山县| 东光县| 水富县| 滦平县| 丰镇市| 祁连县| 奈曼旗| 兴城市| 凤山市| 余庆县| 长宁县| 鄂尔多斯市| 长沙县| 顺昌县| 偃师市| 邮箱| 将乐县| 勃利县| 通辽市| 奇台县| 商丘市| 招远市| 沽源县| 盐亭县| 东丽区| 兴海县| 南皮县|