日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python print 输出到txt_(Python基础教程之七)Python字符串操作

發布時間:2024/7/5 python 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python print 输出到txt_(Python基础教程之七)Python字符串操作 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
  • Python基礎教程
  • 在SublimeEditor中配置Python環境
  • Python代碼中添加注釋
  • Python中的變量的使用
  • Python中的數據類型
  • Python中的關鍵字
  • Python字符串操作
  • Python中的list操作
  • Python中的Tuple操作
  • Pythonmax()和min()–在列表或數組中查找最大值和最小值
  • Python找到最大的N個(前N個)或最小的N個項目
  • Python讀寫CSV文件
  • Python中使用httplib2–HTTPGET和POST示例
  • Python將tuple開箱為變量或參數
  • Python開箱Tuple–太多值無法解壓
  • Pythonmultidict示例–將單個鍵映射到字典中的多個值
  • PythonOrderedDict–有序字典
  • Python字典交集–比較兩個字典
  • Python優先級隊列示例
  • 在Python中,string文字是:

    • 代表Unicode字符的字節數組
    • 用單引號或雙引號引起來
    • 無限長度

    字符串文字

    str = 'hello world'str = "hello world"

    一個多行字符串使用三個單引號或三個雙引號創建的。

    多行字符串文字

    str = '''Say helloto pythonprogramming'''str = """Say helloto pythonprogramming"""

    Python沒有字符數據類型,單個字符就是長度為1的字符串。

    2. Substring or slicing

    通過使用slice語法,我們可以獲得一系列字符。索引從零開始。

    str[m:n] 從位置2(包括)到5(不包括)返回字符串。

    從索引2到5的子字符串

    str = 'hello world'print(str[2:5]) # llo

    負切片將從末尾返回子字符串。

    子串從索引-5到-2

    str = 'hello world'print(str[-5:-2])?? # worstr[-m:-n] 將字符串從位置-5(不包括)返回到-2(包括)。

    3. Strings as arrays

    在python中,字符串表現為數組。方括號可用于訪問字符串的元素。

    字符在第n個位置

    str = 'hello world'print(str[0])?? # hprint(str[1])?? # eprint(str[2])?? # lprint(str[20])? # IndexError: string index out of range

    4. String length

    len()函數返回字符串的長度:

    字符串長度

    str = 'hello world'print(len(str)) # 11

    5. String Formatting

    要在python中格式化s字符串,請{ }在所需位置在字符串中使用占位符。將參數傳遞給format()函數以使用值格式化字符串。

    我們可以在占位符中傳遞參數位置(從零開始)。

    字符串格式()

    age = 36name = 'Lokesh'txt = "My name is {} and my age is {}"print(txt.format(name, age))??? # My name is Lokesh and my age is 36txt = "My age is {1} and the name is {0}"print(txt.format(name, age))??? # My age is 36 and the name is Lokesh

    6. String Methods

    6.1. capitalize()

    它返回一個字符串,其中給定字符串的第一個字符被轉換為大寫。當第一個字符為非字母時,它將返回相同的字符串。

    字符串大寫()

    name = 'lokesh gupta'print( name.capitalize() )? # Lokesh guptatxt = '38 yrs old lokesh gupta'print( txt.capitalize() )?? # 38 yrs old lokesh gupta

    6.2. casefold()

    它返回一個字符串,其中所有字符均為給定字符串的小寫字母。

    字符串casefold()

    txt = 'My Name is Lokesh Gupta'print( txt.casefold() ) # my name is lokesh gupta

    6.3. center()

    使用指定的字符(默認為空格)作為填充字符,使字符串居中對齊。

    在給定的示例中,輸出總共需要20個字符,而“ hello world”位于其中。

    字符串中心

    txt = "hello world"x = txt.center(20)print(x)??? # '??? hello world???? '

    6.4. count()

    它返回指定值出現在字符串中的次數。它有兩種形式:

    count(value) - value to search for in the string. count(value, start, end) - value to search for in the string, where search starts from start position till end position.

    字符串數()

    txt = "hello world"print( txt.count("o") )???????? # 2print( txt.count("o", 4, 7) )?? # 1

    6.5. encode()

    它使用指定的編碼對字符串進行編碼。如果未指定編碼,UTF-8將使用。

    字符串encode()

    txt = "My name is ?mber"x = txt.encode()print(x)??? # b'My name is xc3xa5mber'

    6.6. endswith()

    True如果字符串以指定值結尾,則返回,否則返回False。

    字符串endswith()

    txt = "hello world"print( txt.endswith("world") )????? # Trueprint( txt.endswith("planet") )???? # False

    6.7. expandtabs()

    它將制表符大小設置為指定的空格數。

    字符串expandtabs()

    txt = "helloworld"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()如果找不到該值,該方法將引發異常。

    字符串find()

    txt = "My name is Lokesh Gupta"x = txt.find("e")print(x)??????? # 6

    6.9. format()

    它格式化指定的字符串,并在字符串的占位符內插入參數值。

    字符串格式()

    age = 36name = '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()

    它用于返回字典鍵的值,以格式化帶有命名占位符的字符串。

    字符串format_map()

    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()

    • 它在給定的字符串中查找指定值的第一次出現。
    • 如果找不到要搜索的值,則會引發異常。

    字符串index()

    txt = "My name is Lokesh Gupta"x = txt.index("e")print(x)??????? # 6x = txt.index("z")? # ValueError: substring not found

    6.12. isalnum()

    它檢查字母數字字符串。True如果所有字符都是字母數字,即字母(a-zA-Z)和數字,它將返回(0-9)。

    字符串isalnum()

    print("LokeshGupta".isalnum())????? # Trueprint("Lokesh Gupta".isalnum())???? # False - Contains space

    6.13. isalpha()

    True如果所有字符都是字母,則返回它,即字母(a-zA-Z)。

    字符串isalpha()

    print("LokeshGupta".isalpha())????????? # Trueprint("Lokesh Gupta".isalpha())???????? # False - Contains spaceprint("LokeshGupta38".isalpha())??????? # False - Contains numbers

    6.14. isdecimal()

    如果所有字符均為小數(0-9),則返回代碼。否則返回False。

    字符串isdecimal()

    print("LokeshGupta".isdecimal())??? # Falseprint("12345".isdecimal())????????? # Trueprint("123.45".isdecimal())???????? # False - Contains 'point'print("1234 5678".isdecimal())????? # False - Contains space

    6.15. isdigit()

    True如果所有字符都是數字,則返回,否則返回False。指數也被認為是數字。

    字符串isdigit()

    print("LokeshGupta".isdigit())????? # Falseprint("12345".isdigit())??????????? # Trueprint("123.45".isdigit())?????????? # False - contains decimal pointprint("12342".isdigit())?????? # True - unicode for square 2

    6.16. isidentifier()

    True如果字符串是有效的標識符,則返回,否則返回False。

    有效的標識符僅包含字母數字字母(a-z)和(0-9)或下劃線( _ )。它不能以數字開頭或包含任何空格。

    字符串isidentifier()

    print( "Lokesh_Gupta_38".isidentifier() )?????? # Trueprint( "38_Lokesh_Gupta".isidentifier() )?????? # False - Start with numberprint( "_Lokesh_Gupta".isidentifier() )???????? # Trueprint( "Lokesh Gupta 38".isidentifier() )?????? # False - Contain spaces

    6.17. islower()

    True如果所有字符均小寫,則返回,否則返回False。不檢查數字,符號和空格,僅檢查字母字符。

    字符串islower()

    print( "LokeshGupta".islower() )??????? # Falseprint( "lokeshgupta".islower() )??????? # Trueprint( "lokesh_gupta".islower() )?????? # Trueprint( "lokesh_gupta_38".islower() )??? # True

    6.18. isnumeric()

    True如果所有字符都是數字(0-9),則it方法返回,否則返回False。指數也被認為是數值。

    字符串isnumeric()

    print("LokeshGupta".isnumeric())??? # Falseprint("12345".isnumeric())????????? # Trueprint("123.45".isnumeric())???????? # False - contains decimal pointprint("12342".isnumeric())???? # True - unicode for square 2

    6.19. isprintable()

    它返回True,如果所有字符都打印,否則返回False。不可打印字符用于指示某些格式化操作,例如:

    • 空白(被視為不可見的圖形)
    • 回車
    • 標簽
    • 換行
    • 分頁符
    • 空字符

    字符串isprintable()

    print("LokeshGupta".isprintable())????? # Trueprint("Lokesh Gupta".isprintable())???? # Trueprint("LokeshGupta".isprintable())??? # False

    6.20. isspace()

    True如果字符串中的所有字符都是空格,則返回,否則返回False。

    6.21. istitle()

    它返回True如果文本的所有單詞以大寫字母開頭,字的其余均為小寫字母,即標題案例。否則False。

    字符串istitle()

    print("Lokesh Gupta".istitle())???? # Trueprint("Lokesh gupta".istitle())???? # False

    6.22. isupper()

    True如果所有字符均大寫,則返回,否則返回False。不檢查數字,符號和空格,僅檢查字母字符。

    字符串isupper()

    print("LOKESHGUPTA".isupper())????? # Trueprint("LOKESH GUPTA".isupper())???? # Trueprint("Lokesh Gupta".isupper())???? # False

    6.23. join()

    它以可迭代方式獲取所有項目,并使用強制性指定的分隔符將它們連接為一個字符串。

    字符串join()

    myTuple = ("Lokesh", "Gupta", "38")x = "#".join(myTuple)print(x)??? # Lokesh#Gupta#38

    6.24. ljust()

    此方法將使用指定的字符(默認為空格)作為填充字符使字符串左對齊。

    字符串ljust()

    txt = "lokesh"x = txt.ljust(20, "-")print(x)??? # lokesh--------------

    6.25. lower()

    它返回一個字符串,其中所有字符均為小寫。符號和數字將被忽略。

    字符串lower()

    txt = "Lokesh Gupta"x = txt.lower()print(x)??? # lokesh gupta

    6.26. lstrip()

    它刪除所有前導字符(默認為空格)。

    字符串lstrip()

    txt = "#Lokesh Gupta"x = txt.lstrip("#_,.")print(x)??? # Lokesh Gupta

    6.27. maketrans()

    它創建一個字符到其轉換/替換的一對一映射。當在translate()方法中使用時,此翻譯映射隨后用于將字符替換為其映射的字符。

    字符串maketrans()

    dict = {"a": "123", "b": "456", "c": "789"}string = "abc"print(string.maketrans(dict))?? # {97: '123', 98: '456', 99: '789'}

    6.28. partition()

    它在給定的文本中搜索指定的字符串,并將該字符串拆分為包含三個元素的元組:

    • 第一個元素包含指定字符串之前的部分。
    • 第二個元素包含指定的字符串。
    • 第三個元素包含字符串后面的部分。

    字符串partition()

    txt = "my name is lokesh gupta"x = txt.partition("lokesh")print(x)??? # ('my name is ', 'lokesh', ' gupta')print(x[0]) # my name isprint(x[1]) # lokeshprint(x[2]) #? gupta

    6.29. replace()

    它將指定的短語替換為另一個指定的短語。它有兩種形式:

    • string.replace(oldvalue, newvalue)
    • string.replace(oldvalue, newvalue, count)–“計數”指定要替換的出現次數。默認為所有事件。

    字符串replace()

    txt = "A A A A A"x = txt.replace("A", "B")print(x)??? # B B B B Bx = txt.replace("A", "B", 2)print(x)??? # B B A A A

    6.30. rfind()

    它查找指定值的最后一次出現。-1如果在給定的文本中找不到該值,則返回該值。

    字符串rfind()

    txt = "my name is lokesh gupta"x = txt.rfind("lokesh")????print(x)??????? # 11x = txt.rfind("amit")??????print(x)??????? # -1

    6.31. rindex()

    它查找指定值的最后一次出現,如果找不到該值,則引發異常。

    字符串rindex()

    txt = "my name is lokesh gupta"x = txt.rindex("lokesh")???????print(x)??????????????? # 11x = txt.rindex("amit")? # ValueError: substring not found

    6.32. rjust()

    它將使用指定的字符(默認為空格)作為填充字符來右對齊字符串。

    字符串rjust()

    txt = "lokesh"x = txt.rjust(20,"#")print(x, "is my name")? # ##############lokesh is my name

    6.33. rpartition()

    它搜索指定字符串的最后一次出現,并將該字符串拆分為包含三個元素的元組。

    • 第一個元素包含指定字符串之前的部分。
    • 第二個元素包含指定的字符串。
    • 第三個元素包含字符串后面的部分。

    字符串rpartition()

    txt = "my name is lokesh gupta"x = txt.rpartition("lokesh")print(x)??? # ('my name is ', 'lokesh', ' gupta')print(x[0]) # my name isprint(x[1]) # lokeshprint(x[2]) #? gupta

    6.34. rsplit()

    它將字符串從右開始拆分為一個列表。

    字符串rsplit()

    txt = "apple, banana, cherry"x = txt.rsplit(", ")print(x)??? # ['apple', 'banana', 'cherry']

    6.35. rstrip()

    它刪除所有結尾字符(字符串末尾的字符),空格是默認的結尾字符。

    字符串rstrip()

    txt = "???? lokesh???? "x = txt.rstrip()print(x)??? # '???? lokesh'

    6.36. split()

    它將字符串拆分為列表。您可以指定分隔符。默認分隔符為空格。

    字符串split()

    txt = "my name is lokesh"x = txt.split()print(x)??? # ['my', 'name', 'is', 'lokesh']

    6.37. splitlines()

    通過在換行符處進行拆分,它將字符串拆分為列表。

    字符串splitlines()

    txt = "my nameis lokesh"x = txt.splitlines()print(x)??? # ['my name', 'is lokesh']

    6.38. startswith()

    True如果字符串以指定值開頭,則返回,否則返回False。字符串比較區分大小寫。

    字符串startswith()

    txt = "my name is lokesh"print( txt.startswith("my") )?? # Trueprint( txt.startswith("My") )?? # False

    6.39. strip()

    它將刪除所有前導(開頭的空格)和結尾(結尾的空格)字符(默認為空格)。

    字符串strip()

    txt = "?? my name is lokesh?? "print( txt.strip() )??? # 'my name is lokesh'

    6.40. swapcase()

    它返回一個字符串,其中所有大寫字母均為小寫字母,反之亦然。

    字符串swapcase()

    txt = "My Name Is Lokesh Gupta"print( txt.swapcase() ) # mY nAME iS lOKESH gUPTA

    6.41. title()

    它返回一個字符串,其中每個單詞的第一個字符均為大寫。如果單詞開頭包含數字或符號,則其后的第一個字母將轉換為大寫字母。

    字符串標題()

    print( "lokesh gupta".title() ) # Lokesh Guptaprint( "38lokesh gupta".title() )?? # 38Lokesh Guptaprint( "1. lokesh gupta".title() )? # Lokesh Gupta

    6.42. translate()

    它需要轉換表來根據映射表替換/翻譯給定字符串中的字符。

    字符串translate()

    translation = {97: None, 98: None, 99: 105}string = "abcdef"??print( string.translate(translation) )? # idef

    6.43. upper()

    它返回一個字符串,其中所有字符均為大寫。符號和數字將被忽略。

    字符串upper()

    txt = "lokesh gupta"print( txt.upper() )??? # LOKESH GUPTA

    6.44. zfill()

    它在字符串的開頭添加零(0),直到達到指定的長度。

    字符串zfill()

    txt = "100"x = txt.zfill(10)print( 0000000100 ) # 0000000100

    學習愉快!

    作者:分布式編程 出處:https://zthinker.com/ 如果你喜歡本文,請長按二維碼,關注 分布式編程 .

    總結

    以上是生活随笔為你收集整理的python print 输出到txt_(Python基础教程之七)Python字符串操作的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。