竹内纱里奈和大战黑人_欧美成人黄色小视频_91福利影视_欧美在线观看视频网站_h色网站免费观看_97综合

極客小將

您現(xiàn)在的位置是:首頁 » python編程資訊

資訊內(nèi)容

了解Python中的字符串是什么嗎?

極客小將2021-03-16-
簡介摘要:本文將告訴您Python中的字符串是什么,并向您簡要介紹有關(guān)該概念的所有知識。本文將介紹以下內(nèi)容:如何創(chuàng)建一個字符串?如何從字符串訪問字符?格式化字符串因此,讓我們開始吧。什么是Python中的字符串?我們許多熟悉C,C++等編程語言的人都會得到諸如“字符串是字符的集合或字符數(shù)組”的答案。在P
aM6少兒編程網(wǎng)-https://www.pxcodes.com

摘要:本文將告訴您python中的字符串是什么,并向您簡要介紹有關(guān)該概念的所有知識。

本文將介紹以下內(nèi)容:aM6少兒編程網(wǎng)-https://www.pxcodes.com

如何創(chuàng)建一個字符串?如何從字符串訪問字符?格式化字符串

因此,讓我們開始吧。aM6少兒編程網(wǎng)-https://www.pxcodes.com

什么是Python中的字符串?aM6少兒編程網(wǎng)-https://www.pxcodes.com

我們許多熟悉C,C ++等編程語言的人都會得到諸如“字符串是字符的集合或字符數(shù)組”的答案。aM6少兒編程網(wǎng)-https://www.pxcodes.com

在Python中也是如此,我們說的是String數(shù)據(jù)類型的相同定義。字符串是序列字符的數(shù)組,并寫在單引號,雙引號或三引號內(nèi)。另外,Python沒有字符數(shù)據(jù)類型,因此當(dāng)我們編寫“ a”時,它將被視為長度為1的字符串。aM6少兒編程網(wǎng)-https://www.pxcodes.com

繼續(xù)本文,了解什么是Python中的String?aM6少兒編程網(wǎng)-https://www.pxcodes.com

如何創(chuàng)建一個字符串?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)我們在字符串中同時使用單引號和雙引號以及要編寫多行句子時,通常使用三引號。aM6少兒編程網(wǎng)-https://www.pxcodes.com

筆記aM6少兒編程網(wǎng)-https://www.pxcodes.com

我們需要注意的是,在使用單引號時,字符串中不應(yīng)包含單引號,因為如果發(fā)生這種情況,Python將假定該行在第二個引號本身出現(xiàn)的情況下結(jié)束,并且不會獲取所需的輸出。相同的符號后應(yīng)加上雙引號和三引號。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è)我們要訪問字符串中的一個字符,比方說**后一個字符,我們需要知道它在字符串中的位置。aM6少兒編程網(wǎng)-https://www.pxcodes.com

aM6少兒編程網(wǎng)-https://www.pxcodes.com

這是一個字符串以及分配的位置。因此,如果要從字符串訪問'n',則必須轉(zhuǎn)到第5位。aM6少兒編程網(wǎng)-https://www.pxcodes.com

編號或索引從0到小于字符串長度的1開始。aM6少兒編程網(wǎng)-https://www.pxcodes.com

這是一個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確實是colaM6少兒編程網(wǎng)-https://www.pxcodes.com

現(xiàn)在,如果在索引中從左到右遵循遞增順序模式,則從右到左遵循遞減順序模式,即從-1,-2,-3等。因此,如果要訪問**后一個字符,可以通過兩種方式進(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]的長度。
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'對象不支持項目分配aM6少兒編程網(wǎng)-https://www.pxcodes.com

流程以退出代碼1完成aM6少兒編程網(wǎng)-https://www.pxcodes.com

但是,您可以使用del運算符刪除整個字符串。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

格式化字符串意味著可以在任意位置動態(tài)分配字符串。aM6少兒編程網(wǎng)-https://www.pxcodes.com

可以使用 format()方法來格式化Python中的字符串,該方法是用于格式化字符串的功能非常強(qiáng)大的工具。String中的Format方法包含大括號{}作為占位符,可以根據(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}  {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

左對齊,居中對齊和右對齊,格式為:
|您好| 到| 泰拉|aM6少兒編程網(wǎng)-https://www.pxcodes.com

可以使用格式方法將字符串左對齊(<),右對齊(>)或居中(^)。aM6少兒編程網(wǎng)-https://www.pxcodes.com

{:<10} .format(“ Hello”)表示Python將為該字符串保留10個空間,并且該字符串將從左側(cè)開始。右對齊和居中對齊也是如此。aM6少兒編程網(wǎng)-https://www.pxcodes.com

我希望您能很好地學(xué)習(xí)這些概念,并嘗試使其更加準(zhǔn)確。aM6少兒編程網(wǎng)-https://www.pxcodes.com

相關(guān)免費學(xué)習(xí)推薦:python視頻教程aM6少兒編程網(wǎng)-https://www.pxcodes.com

以上就是了解Python中的字符串是什么嗎?的詳細(xì)內(nèi)容,更多請關(guān)注少兒編程網(wǎng)其它相關(guān)文章!aM6少兒編程網(wǎng)-https://www.pxcodes.com

預(yù)約試聽課

已有385人預(yù)約都是免費的,你也試試吧...

主站蜘蛛池模板: 成人黄色av网站 | 少妇精品久久久一区二区三区 | 国产一级在线观看 | 久久久国产精品一区二区三区 | 亚洲午夜免费视频 | 国产最新在线 | 欧美精品久久久久久久免费软件 | 在线国产区 | 欧美福利网| 欧美国产日韩在线观看 | 欧美黄色一级 | 精品久久久国产 | 久久国产精 | 91精品国产综合久久福利不卡 | 国产91在线播放精品91 | 久久精品久久久精品美女 | 国产精品久久久av久久久 | 欧美aa| 色综合久久天天综合网 | 看全色黄大色黄女片18女人 | 在线视频导航 | 淫男乱女 笨蛋英子 | 国产精品2 | 四虎影院最新网址 | 麻豆精品国产传媒mv男同 | 国产精品播放 | 午夜在线精品偷拍 | 一区二区三区久久 | 国产欧美日韩在线观看 | 国产永久免费观看 | 99视频精品在线 | 精品久久久久久久久久久久 | 欧美成人福利 | 国产精品国产三级国产普通话三级 | 精品一区二区久久 | 亚洲精品日韩精品 | 久久精品一级 | 嫩草国产 | 国产猛男猛女超爽免费视频 | 国产毛片在线 | 欧美一区二区三区的 |