<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                # Python – 字符串 > 原文: [https://howtodoinjava.com/python/python-strings/](https://howtodoinjava.com/python/python-strings/) 在 [Python](https://howtodoinjava.com/python-tutorial/) 中,`string`字面值是: * 代表 Unicode 字符的字節數組 * 用單引號或雙引號引起來 * 無限長度 ```py str = 'hello world' str = "hello world" ``` 使用三個單引號或三個雙引號創建**多行字符串**。 ```py str = '''Say hello to python programming''' str = """Say hello to python programming""" ``` > Python 沒有字符數據類型,單個字符就是長度為 1 的字符串。 ## 2.子串或切片 通過使用切片語法,我們可以獲得一系列字符。 索引從零開始。 `str[m:n]`從位置 2(包括)到 5(不包括)返回字符串。 ```py str = 'hello world' print(str[2:5]) # llo ``` **負切片**從末尾返回子字符串。 ```py str = 'hello world' print(str[-5:-2]) # wor ``` `str[-m:-n]`將字符串從位置 -5(不包括)返回到 -2(包括)。 ## 3.字符串作為數組 在 python 中,字符串表現為數組。 方括號可用于訪問字符串的元素。 ```py str = 'hello world' print(str[0]) # h print(str[1]) # e print(str[2]) # l print(str[20]) # IndexError: string index out of range ``` ## 4.字符串長度 `len()`函數返回字符串的長度: ```py str = 'hello world' print(len(str)) # 11 ``` ## 5.字符串格式化 要在 python 中格式化字符串,請在所需位置在字符串中使用占位符`{ }`。 將參數傳遞給`format()`函數以使用值格式化字符串。 我們可以在占位符中傳遞參數位置(從零開始)。 ```py age = 36 name = 'Lokesh' txt = "My name is {} and my age is {}" print(txt.format(name, age)) # My name is Lokesh and my age is 36 txt = "My age is {1} and the name is {0}" print(txt.format(name, age)) # My age is 36 and the name is Lokesh ``` ## 6.字符串方法 #### 6.1 `capitalize()` 它返回一個字符串,其中給定字符串的第一個字符被轉換為大寫。 當第一個字符為非字母時,它將返回相同的字符串。 ```py name = 'lokesh gupta' print( name.capitalize() ) # Lokesh gupta txt = '38 yrs old lokesh gupta' print( txt.capitalize() ) # 38 yrs old lokesh gupta ``` #### 6.2 `casefold()` 它返回一個字符串,其中所有字符均為給定字符串的小寫字母。 ```py txt = 'My Name is Lokesh Gupta' print( txt.casefold() ) # my name is lokesh gupta ``` #### 6.3 `center()` 使用指定的字符(默認為空格)作為填充字符,使字符串居中對齊。 在給定的示例中,輸出總共需要 20 個字符,而“ hello world”位于其中。 ```py txt = "hello world" x = txt.center(20) print(x) # ' hello world ' ``` #### 6.4 `count()` 它返回指定值出現在字符串中的次數。 它有兩種形式: `count(value)` - 要在字符串中搜索的`value` `count(value, start, end)` - 要在字符串中搜索的`value`,其中搜索起始于`start`位置,直到`end`位置。 ```py txt = "hello world" print( txt.count("o") ) # 2 print( txt.count("o", 4, 7) ) # 1 ``` #### 6.5 `encode()` 它使用指定的編碼對字符串進行編碼。 如果未指定編碼,將使用`UTF-8`。 ```py txt = "My name is ?mber" x = txt.encode() print(x) # b'My name is \xc3\xa5mber' ``` #### 6.6 [`endswith()`](https://howtodoinjava.com/python/string-endswith-method/) 如果字符串以指定值結尾,則返回`True`,否則返回`False`。 ```py txt = "hello world" print( txt.endswith("world") ) # True print( txt.endswith("planet") ) # False ``` #### 6.7 `expandtabs()` 它將制表符大小設置為指定的空格數。 ```py txt = "hello\tworld" print( txt.expandtabs(2) ) # 'hello world' print( txt.expandtabs(4) ) # 'hello world' print( txt.expandtabs(16) ) # 'hello world' ``` #### 6.8 `find()` 它查找指定值的第一次出現。 如果指定的值不在字符串中,則返回`-1`。 `find()`與`index()`方法相同,唯一的區別是,如果找不到該值,則`index()`方法會引發異常。 ```py txt = "My name is Lokesh Gupta" x = txt.find("e") print(x) # 6 ``` #### 6.9 `format()` 它格式化指定的字符串,并在字符串的占位符內插入參數值。 ```py age = 36 name = 'Lokesh' txt = "My name is {} and my age is {}" print( txt.format(name, age) ) # My name is Lokesh and my age is 36 ``` #### 6.10 `format_map()` 它用于返回字典鍵的值,以格式化帶有命名占位符的字符串。 ```py params = {'name':'Lokesh Gupta', 'age':'38'} txt = "My name is {name} and age is {age}" x = txt.format_map(params) print(x) # My name is Lokesh Gupta and age is 38 ``` #### 6.11 `index()` * 它在給定的字符串中查找指定值的第一次出現。 * 如果找不到要搜索的值,則會引發異常。 ```py txt = "My name is Lokesh Gupta" x = txt.index("e") print(x) # 6 x = txt.index("z") # ValueError: substring not found ``` #### 6.12 `isalnum()` 它檢查字母數字字符串。 如果所有字符均為字母數字,表示字母`(a-zA-Z)`和數字`(0-9)`,則返回`True`。 ```py print("LokeshGupta".isalnum()) # True print("Lokesh Gupta".isalnum()) # False - Contains space ``` #### 6.13 `isalpha()` 如果所有字符都是字母,則返回`True`,表示字母`(a-zA-Z)`。 ```py print("LokeshGupta".isalpha()) # True print("Lokesh Gupta".isalpha()) # False - Contains space print("LokeshGupta38".isalpha()) # False - Contains numbers ``` #### 6.14 `isdecimal()` 如果所有字符均為十進制(0-9),則返回代碼。 否則返回`False`。 ```py print("LokeshGupta".isdecimal()) # False print("12345".isdecimal()) # True print("123.45".isdecimal()) # False - Contains 'point' print("1234 5678".isdecimal()) # False - Contains space ``` #### 6.15 `isdigit()` 如果所有字符都是數字,則返回`True`,否則返回`False`。 指數也被認為是數字。 ```py print("LokeshGupta".isdigit()) # False print("12345".isdigit()) # True print("123.45".isdigit()) # False - contains decimal point print("1234\u00B2".isdigit()) # True - unicode for square 2 ``` #### 6.16 `isidentifier()` 如果字符串是有效的標識符,則返回`True`,否則返回`False`。 有效的標識符僅包含字母數字字母`(a-z)`和`(0-9)`或下劃線`( _ )`。 它不能以數字開頭或包含任何空格。 ```py print( "Lokesh_Gupta_38".isidentifier() ) # True print( "38_Lokesh_Gupta".isidentifier() ) # False - Start with number print( "_Lokesh_Gupta".isidentifier() ) # True print( "Lokesh Gupta 38".isidentifier() ) # False - Contain spaces ``` #### 6.17 `islower()` 如果所有字符均小寫,則返回`True`,否則返回`False`。 不檢查數字,符號和空格,僅檢查字母字符。 ```py print( "LokeshGupta".islower() ) # False print( "lokeshgupta".islower() ) # True print( "lokesh_gupta".islower() ) # True print( "lokesh_gupta_38".islower() ) # True ``` #### 6.18 `isnumeric()` 如果所有字符都是數字(`0-9`),則此方法返回`True`,否則返回`False`。 指數也被認為是數值。 ```py print("LokeshGupta".isnumeric()) # False print("12345".isnumeric()) # True print("123.45".isnumeric()) # False - contains decimal point print("1234\u00B2".isnumeric()) # True - unicode for square 2 ``` #### 6.19 `isprintable()` 如果所有字符均可打印,則返回`True`,否則返回 False。 不可打印字符用于指示某些格式化操作,例如: * 空格(被視為不可見的圖形) * 回車 * 制表 * 換行 * 分頁符 * 空字符 ```py print("LokeshGupta".isprintable()) # True print("Lokesh Gupta".isprintable()) # True print("Lokesh\tGupta".isprintable()) # False ``` #### 6.20 `isspace()` 如果字符串中的所有字符都是空格,則返回`True`,否則返回`False`。 #### 6.21 `istitle()` 如果文本中的所有單詞均以大寫字母開頭,而其余單詞均為小寫字母(即標題大小寫),則返回`True`。 否則`False`。 ```py print("Lokesh Gupta".istitle()) # True print("Lokesh gupta".istitle()) # False ``` #### 6.22 `isupper()` 如果所有字符均大寫,則返回`True`,否則返回`False`。 不檢查數字,符號和空格,僅檢查字母字符。 ```py print("LOKESHGUPTA".isupper()) # True print("LOKESH GUPTA".isupper()) # True print("Lokesh Gupta".isupper()) # False ``` #### 6.23 `join()` 它以可迭代方式獲取所有項目,并使用強制性指定的分隔符將它們連接為一個字符串。 ```py myTuple = ("Lokesh", "Gupta", "38") x = "#".join(myTuple) print(x) # Lokesh#Gupta#38 ``` #### 6.24 `ljust()` 此方法將使用指定的字符(默認為空格)作為填充字符使字符串左對齊。 ```py txt = "lokesh" x = txt.ljust(20, "-") print(x) # lokesh-------------- ``` #### 6.25 `lower()` 它返回一個字符串,其中所有字符均為小寫。 符號和數字將被忽略。 ```py txt = "Lokesh Gupta" x = txt.lower() print(x) # lokesh gupta ``` #### 6.26 `lstrip()` 它刪除所有前導字符(默認為空格)。 ```py txt = "#Lokesh Gupta" x = txt.lstrip("#_,.") print(x) # Lokesh Gupta ``` #### 6.27 `maketrans()` 它創建一個字符到其轉換/替換的一對一映射。 當在`translate()`方法中使用時,此轉換映射用于將字符替換為其映射的字符。 ```py dict = {"a": "123", "b": "456", "c": "789"} string = "abc" print(string.maketrans(dict)) # {97: '123', 98: '456', 99: '789'} ``` #### 6.28 `partition()` 它在給定的文本中搜索指定的字符串,并將該字符串拆分為包含三個元素的元組: * 第一個元素包含指定字符串之前的部分。 * 第二個元素包含指定的字符串。 * 第三個元素包含字符串后面的部分。 ```py txt = "my name is lokesh gupta" x = txt.partition("lokesh") print(x) # ('my name is ', 'lokesh', ' gupta') print(x[0]) # my name is print(x[1]) # lokesh print(x[2]) # gupta ``` #### 6.29 `replace()` 它將指定的短語替換為另一個指定的短語。 它有兩種形式: * `string.replace(oldvalue, newvalue)` * `string.replace(oldvalue, newvalue, count)` – `count`指定要替換的匹配次數。 默認為所有事件。 ```py txt = "A A A A A" x = txt.replace("A", "B") print(x) # B B B B B x = txt.replace("A", "B", 2) print(x) # B B A A A ``` #### 6.30 `rfind()` 它查找指定值的最后一次出現。 如果在給定的文本中找不到該值,則返回`-1`。 ```py txt = "my name is lokesh gupta" x = txt.rfind("lokesh") print(x) # 11 x = txt.rfind("amit") print(x) # -1 ``` #### 6.31 `rindex()` 它查找指定值的最后一次出現,如果找不到該值,則會引發異常。 ```py txt = "my name is lokesh gupta" x = txt.rindex("lokesh") print(x) # 11 x = txt.rindex("amit") # ValueError: substring not found ``` #### 6.32 `rjust()` 它將使用指定的字符(默認為空格)作為填充字符來右對齊字符串。 ```py txt = "lokesh" x = txt.rjust(20,"#") print(x, "is my name") # ##############lokesh is my name ``` #### 6.33 `rpartition()` 它搜索指定字符串的最后一次出現,并將該字符串拆分為包含三個元素的元組。 * 第一個元素包含指定字符串之前的部分。 * 第二個元素包含指定的字符串。 * 第三個元素包含字符串后面的部分。 ```py txt = "my name is lokesh gupta" x = txt.rpartition("lokesh") print(x) # ('my name is ', 'lokesh', ' gupta') print(x[0]) # my name is print(x[1]) # lokesh print(x[2]) # gupta ``` #### 6.34 `rsplit()` 它將字符串從右開始拆分為列表。 ```py txt = "apple, banana, cherry" x = txt.rsplit(", ") print(x) # ['apple', 'banana', 'cherry'] ``` #### 6.35 `rstrip()` 它刪除所有結尾字符(字符串末尾的字符),空格是默認的結尾字符。 ```py txt = " lokesh " x = txt.rstrip() print(x) # ' lokesh' ``` #### 6.36 [`split()`](https://howtodoinjava.com/python/split-string/) 它將字符串拆分為列表。 您可以指定分隔符。 默認分隔符為空格。 ```py txt = "my name is lokesh" x = txt.split() print(x) # ['my', 'name', 'is', 'lokesh'] ``` #### 6.37 `splitlines()` 通過在換行符處進行拆分,它將字符串拆分為列表。 ```py txt = "my name\nis lokesh" x = txt.splitlines() print(x) # ['my name', 'is lokesh'] ``` #### 6.38 [`startswith()`](https://howtodoinjava.com/python/string-startswith/) 如果字符串以指定值開頭,則返回`True`,否則返回`False`。 字符串比較區分大小寫。 ```py txt = "my name is lokesh" print( txt.startswith("my") ) # True print( txt.startswith("My") ) # False ``` #### 6.39 `strip()` 它將刪除所有前導(開頭的空格)和結尾(結尾的空格)字符(默認為空格)。 ```py txt = " my name is lokesh " print( txt.strip() ) # 'my name is lokesh' ``` #### 6.40 `swapcase()` 它返回一個字符串,其中所有大寫字母均為小寫字母,反之亦然。 ```py txt = "My Name Is Lokesh Gupta" print( txt.swapcase() ) # mY nAME iS lOKESH gUPTA ``` #### 6.41 `title()` 它返回一個字符串,其中每個單詞的第一個字符均為大寫。 如果單詞開頭包含數字或符號,則其后的第一個字母將轉換為大寫字母。 ```py print( "lokesh gupta".title() ) # Lokesh Gupta print( "38lokesh gupta".title() ) # 38Lokesh Gupta print( "1\. lokesh gupta".title() ) # Lokesh Gupta ``` #### 6.42 `translate()` 它需要轉換表根據映射表替換/轉換給定字符串中的字符。 ```py translation = {97: None, 98: None, 99: 105} string = "abcdef" print( string.translate(translation) ) # idef ``` #### 6.43 `upper()` 它返回一個字符串,其中所有字符均大寫。 符號和數字將被忽略。 ```py txt = "lokesh gupta" print( txt.upper() ) # LOKESH GUPTA ``` #### 6.44 `zfill()` 它在字符串的開頭添加零(0),直到達到指定的長度。 ```py txt = "100" x = txt.zfill(10) print( 0000000100 ) # 0000000100 ``` 學習愉快!
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看