資訊內(nèi)容
了解Python中的字符串是什么嗎?

本文將介紹以下內(nèi)容:aM6少兒編程網(wǎng)-https://www.pxcodes.com
如何創(chuàng)建一個(gè)字符串?如何從字符串訪問字符?格式化字符串因此,讓我們開始吧。aM6少兒編程網(wǎng)-https://www.pxcodes.com
什么是Python中的字符串?aM6少兒編程網(wǎng)-https://www.pxcodes.com
我們?cè)S多熟悉C,C ++等編程語言的人都會(huì)得到諸如“字符串是字符的集合或字符數(shù)組”的答案。aM6少兒編程網(wǎng)-https://www.pxcodes.com
在Python中也是如此,我們說的是String數(shù)據(jù)類型的相同定義。字符串是序列字符的數(shù)組,并寫在單引號(hào),雙引號(hào)或三引號(hào)內(nèi)。另外,Python沒有字符數(shù)據(jù)類型,因此當(dāng)我們編寫“ a”時(shí),它將被視為長(zhǎng)度為1的字符串。aM6少兒編程網(wǎng)-https://www.pxcodes.com
繼續(xù)本文,了解什么是Python中的String?aM6少兒編程網(wǎng)-https://www.pxcodes.com
如何創(chuàng)建一個(gè)字符串?aM6少兒編程網(wǎng)-https://www.pxcodes.com
s = 'Hello' print(s) s1 = "Hello" print(s1) s2 = ''' Hello How is the whether today? ''' print(s2)輸出:aM6少兒編程網(wǎng)-https://www.pxcodes.com
你好
你好
你好
今天如何?aM6少兒編程網(wǎng)-https://www.pxcodes.com
當(dāng)我們?cè)谧址型瑫r(shí)使用單引號(hào)和雙引號(hào)以及要編寫多行句子時(shí),通常使用三引號(hào)。aM6少兒編程網(wǎng)-https://www.pxcodes.com
筆記aM6少兒編程網(wǎng)-https://www.pxcodes.com
我們需要注意的是,在使用單引號(hào)時(shí),字符串中不應(yīng)包含單引號(hào),因?yàn)槿绻l(fā)生這種情況,Python將假定該行在第二個(gè)引號(hào)本身出現(xiàn)的情況下結(jié)束,并且不會(huì)獲取所需的輸出。相同的符號(hào)后應(yīng)加上雙引號(hào)和三引號(hào)。aM6少兒編程網(wǎng)-https://www.pxcodes.com
繼續(xù)本文,了解什么是Python中的String?aM6少兒編程網(wǎng)-https://www.pxcodes.com
如何從字符串訪問字符?aM6少兒編程網(wǎng)-https://www.pxcodes.com
假設(shè)我們要訪問字符串中的一個(gè)字符,比方說**后一個(gè)字符,我們需要知道它在字符串中的位置。aM6少兒編程網(wǎng)-https://www.pxcodes.com
aM6少兒編程網(wǎng)-https://www.pxcodes.com
這是一個(gè)字符串以及分配的位置。因此,如果要從字符串訪問'n',則必須轉(zhuǎn)到第5位。aM6少兒編程網(wǎng)-https://www.pxcodes.com
編號(hào)或索引從0到小于字符串長(zhǎng)度的1開始。aM6少兒編程網(wǎng)-https://www.pxcodes.com
這是一個(gè)python程序,可以使我們更加清楚。aM6少兒編程網(wǎng)-https://www.pxcodes.com
str = 'Antarctica is really cold.' print('str = ', str) #first character print('str[0] = ', str[0]) #last character print('str[-1] = ', str[-1]) #slicing 2nd to 5th character print('str[1:5] = ', str[1:5]) #slicing 6th to 2nd last character print('str[5:-2] = ', str[5:-2])輸出:aM6少兒編程網(wǎng)-https://www.pxcodes.com
str =南極洲真的很冷。
str [0] = A
str [-1] =。
str [1:5] = ntar
str [5:-2] = ctica確實(shí)是colaM6少兒編程網(wǎng)-https://www.pxcodes.com
現(xiàn)在,如果在索引中從左到右遵循遞增順序模式,則從右到左遵循遞減順序模式,即從-1,-2,-3等。因此,如果要訪問**后一個(gè)字符,可以通過兩種方式進(jìn)行。aM6少兒編程網(wǎng)-https://www.pxcodes.com
str = 'Antarctica is really cold.' a = len(str) print('length of str ', a) #last character with the help of length of the string print('str[a] ', str[a-1]) #last character with the help of indexing print('str[-1] ',str[-1])輸出:aM6少兒編程網(wǎng)-https://www.pxcodes.com
str 26
str [a]的長(zhǎng)度。
str [-1]。aM6少兒編程網(wǎng)-https://www.pxcodes.com
字符串本質(zhì)上是不可變的,這意味著一旦聲明了字符串,就不能更改其中的任何字符。aM6少兒編程網(wǎng)-https://www.pxcodes.com
s = "Hello Batman" print(s) s[2] = 'P' print(s)輸出:aM6少兒編程網(wǎng)-https://www.pxcodes.com
你好蝙蝠俠
回溯(**近一次通話**近):
文件“ C:/Users/prac.py”,第3行,位于
s [2] =' P'TypeError
:'str'對(duì)象不支持項(xiàng)目分配aM6少兒編程網(wǎng)-https://www.pxcodes.com
流程以退出代碼1完成aM6少兒編程網(wǎng)-https://www.pxcodes.com
但是,您可以使用del運(yùn)算符刪除整個(gè)字符串。aM6少兒編程網(wǎng)-https://www.pxcodes.com
s = "Hello Batman" print(s) del s print(s)輸出:aM6少兒編程網(wǎng)-https://www.pxcodes.com
您好蝙蝠俠
回溯(**近一次通話**近):
文件“ C:/Users/prac.py”,
打印中的第4行
NameError:未定義名稱“ s”aM6少兒編程網(wǎng)-https://www.pxcodes.com
流程以退出代碼1完成aM6少兒編程網(wǎng)-https://www.pxcodes.com
如果您不希望s是“ Hello Batman”,而希望它是其他字符串,則可以將字符串整體進(jìn)行更新。aM6少兒編程網(wǎng)-https://www.pxcodes.com
s = "Hello Batman" print(s) s = "Hello Spiderman" print(s)輸出:aM6少兒編程網(wǎng)-https://www.pxcodes.com
你好蝙蝠俠
你好蜘蛛俠aM6少兒編程網(wǎng)-https://www.pxcodes.com
繼續(xù)本文,了解什么是Python中的String?aM6少兒編程網(wǎng)-https://www.pxcodes.com
格式化字符串:aM6少兒編程網(wǎng)-https://www.pxcodes.com
格式化字符串意味著可以在任意位置動(dòng)態(tài)分配字符串。aM6少兒編程網(wǎng)-https://www.pxcodes.com
可以使用 format()方法來格式化Python中的字符串,該方法是用于格式化字符串的功能非常強(qiáng)大的工具。String中的Format方法包含大括號(hào){}作為占位符,可以根據(jù)位置或關(guān)鍵字保存參數(shù)以指定順序。aM6少兒編程網(wǎng)-https://www.pxcodes.com
String1 = "{} {} {}".format('Hello', 'to', 'Batman') print("Default order: ") print(String1) # Positional Formatting String1 = "{1} {0} {2}".format('Hello', 'to', 'Batman') print("nPositional order: ") print(String1) # Keyword Formatting String1 = "{c} {b} {a}".format(a='Hello', b='to', c='Spiderman') print("nString in order of Keywords: ") print(String1) # Formatting of Integers String1 = "{0:b}".format(20) print("binary representation of 20 is ") print(String1) # Formatting of Floats String1 = "{0:e}".format(188.996) print("nExponent representation of 188.996 is ") print(String1) # Rounding off Integers String1 = "{0:.2f}".format(1 / 6) print("none-sixth is : ") print(String1) # String alignment String1 = "|{:<10}|{:^10}|{:>10}|".format('Hello', 'to', 'Tyra') print("nLeft, centre and right alignment with Formatting: ") print(String1)輸出:aM6少兒編程網(wǎng)-https://www.pxcodes.com
默認(rèn)順序:
蝙蝠俠向您問好aM6少兒編程網(wǎng)-https://www.pxcodes.com
位置順序:
致Hello BatmanaM6少兒編程網(wǎng)-https://www.pxcodes.com
字符串按關(guān)鍵字順序排列:
蜘蛛俠到HelloaM6少兒編程網(wǎng)-https://www.pxcodes.com
20的二進(jìn)制表示形式是10100aM6少兒編程網(wǎng)-https://www.pxcodes.com
188.996的指數(shù)表示為
1.889960e + 02aM6少兒編程網(wǎng)-https://www.pxcodes.com
六分之一是:
0.17aM6少兒編程網(wǎng)-https://www.pxcodes.com
左對(duì)齊,居中對(duì)齊和右對(duì)齊,格式為:
|您好| 到| 泰拉|aM6少兒編程網(wǎng)-https://www.pxcodes.com
可以使用格式方法將字符串左對(duì)齊(<),右對(duì)齊(>)或居中(^)。aM6少兒編程網(wǎng)-https://www.pxcodes.com
{:<10} .format(“ Hello”)表示Python將為該字符串保留10個(gè)空間,并且該字符串將從左側(cè)開始。右對(duì)齊和居中對(duì)齊也是如此。aM6少兒編程網(wǎng)-https://www.pxcodes.com
我希望您能很好地學(xué)習(xí)這些概念,并嘗試使其更加準(zhǔn)確。aM6少兒編程網(wǎng)-https://www.pxcodes.com
相關(guān)免費(fèi)學(xué)習(xí)推薦:python視頻教程aM6少兒編程網(wǎng)-https://www.pxcodes.com
以上就是了解Python中的字符串是什么嗎?的詳細(xì)內(nèi)容,更多請(qǐng)關(guān)注少兒編程網(wǎng)其它相關(guān)文章!aM6少兒編程網(wǎng)-https://www.pxcodes.com

- 上一篇
python如何判斷回文數(shù)
簡(jiǎn)介python判斷回文數(shù)的方法:首先將數(shù)組轉(zhuǎn)為字符串;然后設(shè)置兩個(gè)指針,一個(gè)從左往右遍歷字符串,一個(gè)從右往左遍歷,如果遇到兩個(gè)不相等的情況,則不為回文數(shù),直到兩個(gè)指針相等。本教程操作環(huán)境:windows7系統(tǒng)、python3.9版,DELLG3電腦。python判斷回文數(shù)的方法:使用python解決非
- 下一篇
python函數(shù)的定義和調(diào)用是什么
簡(jiǎn)介python函數(shù)的定義和調(diào)用:1、使用def關(guān)鍵字定義函數(shù)嗎,代碼為【def函數(shù)名(參數(shù)1,參數(shù)2,參數(shù)3...)】;2、函數(shù)必須先定義,才能調(diào)用,否則會(huì)報(bào)錯(cuò)。本教程操作環(huán)境:windows7系統(tǒng)、python3.9版,DELLG3電腦。python函數(shù)的定義和調(diào)用:一、函數(shù)定義1)使用def關(guān)鍵字