資訊內容
了解Python中的字符串是什么嗎?

本文將介紹以下內容:aM6少兒編程網-https://www.pxcodes.com
如何創建一個字符串?如何從字符串訪問字符?格式化字符串因此,讓我們開始吧。aM6少兒編程網-https://www.pxcodes.com
什么是Python中的字符串?aM6少兒編程網-https://www.pxcodes.com
我們許多熟悉C,C ++等編程語言的人都會得到諸如“字符串是字符的集合或字符數組”的答案。aM6少兒編程網-https://www.pxcodes.com
在Python中也是如此,我們說的是String數據類型的相同定義。字符串是序列字符的數組,并寫在單引號,雙引號或三引號內。另外,Python沒有字符數據類型,因此當我們編寫“ a”時,它將被視為長度為1的字符串。aM6少兒編程網-https://www.pxcodes.com
繼續本文,了解什么是Python中的String?aM6少兒編程網-https://www.pxcodes.com
如何創建一個字符串?aM6少兒編程網-https://www.pxcodes.com
s = 'Hello' print(s) s1 = "Hello" print(s1) s2 = ''' Hello How is the whether today? ''' print(s2)輸出:aM6少兒編程網-https://www.pxcodes.com
你好
你好
你好
今天如何?aM6少兒編程網-https://www.pxcodes.com
當我們在字符串中同時使用單引號和雙引號以及要編寫多行句子時,通常使用三引號。aM6少兒編程網-https://www.pxcodes.com
筆記aM6少兒編程網-https://www.pxcodes.com
我們需要注意的是,在使用單引號時,字符串中不應包含單引號,因為如果發生這種情況,Python將假定該行在第二個引號本身出現的情況下結束,并且不會獲取所需的輸出。相同的符號后應加上雙引號和三引號。aM6少兒編程網-https://www.pxcodes.com
繼續本文,了解什么是Python中的String?aM6少兒編程網-https://www.pxcodes.com
如何從字符串訪問字符?aM6少兒編程網-https://www.pxcodes.com
假設我們要訪問字符串中的一個字符,比方說**后一個字符,我們需要知道它在字符串中的位置。aM6少兒編程網-https://www.pxcodes.com
aM6少兒編程網-https://www.pxcodes.com
這是一個字符串以及分配的位置。因此,如果要從字符串訪問'n',則必須轉到第5位。aM6少兒編程網-https://www.pxcodes.com
編號或索引從0到小于字符串長度的1開始。aM6少兒編程網-https://www.pxcodes.com
這是一個python程序,可以使我們更加清楚。aM6少兒編程網-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少兒編程網-https://www.pxcodes.com
str =南極洲真的很冷。
str [0] = A
str [-1] =。
str [1:5] = ntar
str [5:-2] = ctica確實是colaM6少兒編程網-https://www.pxcodes.com
現在,如果在索引中從左到右遵循遞增順序模式,則從右到左遵循遞減順序模式,即從-1,-2,-3等。因此,如果要訪問**后一個字符,可以通過兩種方式進行。aM6少兒編程網-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少兒編程網-https://www.pxcodes.com
str 26
str [a]的長度。
str [-1]。aM6少兒編程網-https://www.pxcodes.com
字符串本質上是不可變的,這意味著一旦聲明了字符串,就不能更改其中的任何字符。aM6少兒編程網-https://www.pxcodes.com
s = "Hello Batman" print(s) s[2] = 'P' print(s)輸出:aM6少兒編程網-https://www.pxcodes.com
你好蝙蝠俠
回溯(**近一次通話**近):
文件“ C:/Users/prac.py”,第3行,位于
s [2] =' P'TypeError
:'str'對象不支持項目分配aM6少兒編程網-https://www.pxcodes.com
流程以退出代碼1完成aM6少兒編程網-https://www.pxcodes.com
但是,您可以使用del運算符刪除整個字符串。aM6少兒編程網-https://www.pxcodes.com
s = "Hello Batman" print(s) del s print(s)輸出:aM6少兒編程網-https://www.pxcodes.com
您好蝙蝠俠
回溯(**近一次通話**近):
文件“ C:/Users/prac.py”,
打印中的第4行
NameError:未定義名稱“ s”aM6少兒編程網-https://www.pxcodes.com
流程以退出代碼1完成aM6少兒編程網-https://www.pxcodes.com
如果您不希望s是“ Hello Batman”,而希望它是其他字符串,則可以將字符串整體進行更新。aM6少兒編程網-https://www.pxcodes.com
s = "Hello Batman" print(s) s = "Hello Spiderman" print(s)輸出:aM6少兒編程網-https://www.pxcodes.com
你好蝙蝠俠
你好蜘蛛俠aM6少兒編程網-https://www.pxcodes.com
繼續本文,了解什么是Python中的String?aM6少兒編程網-https://www.pxcodes.com
格式化字符串:aM6少兒編程網-https://www.pxcodes.com
格式化字符串意味著可以在任意位置動態分配字符串。aM6少兒編程網-https://www.pxcodes.com
可以使用 format()方法來格式化Python中的字符串,該方法是用于格式化字符串的功能非常強大的工具。String中的Format方法包含大括號{}作為占位符,可以根據位置或關鍵字保存參數以指定順序。aM6少兒編程網-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少兒編程網-https://www.pxcodes.com
默認順序:
蝙蝠俠向您問好aM6少兒編程網-https://www.pxcodes.com
位置順序:
致Hello BatmanaM6少兒編程網-https://www.pxcodes.com
字符串按關鍵字順序排列:
蜘蛛俠到HelloaM6少兒編程網-https://www.pxcodes.com
20的二進制表示形式是10100aM6少兒編程網-https://www.pxcodes.com
188.996的指數表示為
1.889960e + 02aM6少兒編程網-https://www.pxcodes.com
六分之一是:
0.17aM6少兒編程網-https://www.pxcodes.com
左對齊,居中對齊和右對齊,格式為:
|您好| 到| 泰拉|aM6少兒編程網-https://www.pxcodes.com
可以使用格式方法將字符串左對齊(<),右對齊(>)或居中(^)。aM6少兒編程網-https://www.pxcodes.com
{:<10} .format(“ Hello”)表示Python將為該字符串保留10個空間,并且該字符串將從左側開始。右對齊和居中對齊也是如此。aM6少兒編程網-https://www.pxcodes.com
我希望您能很好地學習這些概念,并嘗試使其更加準確。aM6少兒編程網-https://www.pxcodes.com
相關免費學習推薦:python視頻教程aM6少兒編程網-https://www.pxcodes.com
以上就是了解Python中的字符串是什么嗎?的詳細內容,更多請關注少兒編程網其它相關文章!aM6少兒編程網-https://www.pxcodes.com

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