国产日韩欧美一区二区三区综合,日本黄色免费在线,国产精品麻豆欧美日韩ww,色综合狠狠操

極客小將

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

資訊內(nèi)容

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

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

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

本文將介紹以下內(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

預(yù)約試聽課

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

主站蜘蛛池模板: 广平县| 盐源县| 将乐县| 桂阳县| 辽中县| 江达县| 镇赉县| 南开区| 海阳市| 饶阳县| 大安市| 普陀区| 马尔康县| 衡阳县| 凌海市| 疏勒县| 周至县| 邹城市| 揭阳市| 元谋县| 凌海市| 和硕县| 杭锦后旗| 望江县| 昌平区| 吉首市| 洛阳市| 泰宁县| 邛崃市| 泸溪县| 若羌县| 平遥县| 鹤峰县| 丹寨县| 绵竹市| 新晃| 仁布县| 长兴县| 香格里拉县| 呼伦贝尔市| 土默特左旗|