网站屏蔽ip地址,企业网站建设公司名称,怎么在企业站建立网站吗,嘉兴中小企业网站建设在Python中#xff0c;string文字是#xff1a;
代表Unicode字符的字节数组用单引号或双引号引起来无限长度
字符串文字
str hello worldstr hello world一个多行字符串使用三个单引号或三个双引号创建的。
多行字符串文字
str Say helloto pythonprogra…在Python中string文字是
代表Unicode字符的字节数组用单引号或双引号引起来无限长度
字符串文字
str hello worldstr hello world一个多行字符串使用三个单引号或三个双引号创建的。
多行字符串文字
str Say helloto pythonprogrammingstr Say helloto pythonprogrammingPython没有字符数据类型单个字符就是长度为1的字符串。 2. Substring or slicing
通过使用slice语法我们可以获得一系列字符。索引从零开始。
str[m:n] 从位置2包括到5不包括返回字符串。
从索引2到5的子字符串
str hello worldprint(str[2:5]) # llo负切片将从末尾返回子字符串。
子串从索引-5到-2
str hello worldprint(str[-5:-2]) # worstr[-m:-n] 将字符串从位置-5不包括返回到-2包括。3. Strings as arrays
在python中字符串表现为数组。方括号可用于访问字符串的元素。
字符在第n个位置
str hello worldprint(str[0]) # hprint(str[1]) # eprint(str[2]) # lprint(str[20]) # IndexError: string index out of range4. String length
len函数返回字符串的长度
字符串长度
str hello worldprint(len(str)) # 115. String Formatting
要在python中格式化s字符串请{ }在所需位置在字符串中使用占位符。将参数传递给format()函数以使用值格式化字符串。
我们可以在占位符中传递参数位置从零开始。
字符串格式
age 36name Lokeshtxt 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 Lokesh6. String Methods
6.1. capitalize()
它返回一个字符串其中给定字符串的第一个字符被转换为大写。当第一个字符为非字母时它将返回相同的字符串。
字符串大写
name lokesh guptaprint( name.capitalize() ) # Lokesh guptatxt 38 yrs old lokesh guptaprint( txt.capitalize() ) # 38 yrs old lokesh gupta6.2. casefold()
它返回一个字符串其中所有字符均为给定字符串的小写字母。
字符串casefold
txt My Name is Lokesh Guptaprint( txt.casefold() ) # my name is lokesh gupta6.3. center()
使用指定的字符默认为空格作为填充字符使字符串居中对齐。
在给定的示例中输出总共需要20个字符而“ hello world”位于其中。
字符串中心
txt hello worldx 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 worldprint( txt.count(o) ) # 2print( txt.count(o, 4, 7) ) # 16.5. encode()
它使用指定的编码对字符串进行编码。如果未指定编码UTF-8将使用。
字符串encode
txt My name is åmberx txt.encode()print(x) # bMy name is \xc3\xa5mber6.6. endswith()
True如果字符串以指定值结尾则返回否则返回False。
字符串endswith
txt hello worldprint( txt.endswith(world) ) # Trueprint( txt.endswith(planet) ) # False6.7. expandtabs()
它将制表符大小设置为指定的空格数。
字符串expandtabs
txt hello\tworldprint( txt.expandtabs(2) ) # hello worldprint( txt.expandtabs(4) ) # hello worldprint( txt.expandtabs(16) ) # hello world6.8. find()
它查找指定值的第一次出现。-1如果字符串中没有指定的值它将返回。
find()与index()方法相同唯一的区别是index()如果找不到该值该方法将引发异常。
字符串find
txt My name is Lokesh Guptax txt.find(e)print(x) # 66.9. format()
它格式化指定的字符串并在字符串的占位符内插入参数值。
字符串格式
age 36name Lokeshtxt My name is {} and my age is {}print( txt.format(name, age) ) # My name is Lokesh and my age is 366.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 386.11. index()
它在给定的字符串中查找指定值的第一次出现。如果找不到要搜索的值则会引发异常。
字符串index
txt My name is Lokesh Guptax txt.index(e)print(x) # 6x txt.index(z) # ValueError: substring not found6.12. isalnum()
它检查字母数字字符串。True如果所有字符都是字母数字即字母(a-zA-Z)和数字它将返回(0-9)。
字符串isalnum
print(LokeshGupta.isalnum()) # Trueprint(Lokesh Gupta.isalnum()) # False - Contains space6.13. isalpha()
True如果所有字符都是字母则返回它即字母(a-zA-Z)。
字符串isalpha
print(LokeshGupta.isalpha()) # Trueprint(Lokesh Gupta.isalpha()) # False - Contains spaceprint(LokeshGupta38.isalpha()) # False - Contains numbers6.14. isdecimal()
如果所有字符均为小数0-9则返回代码。否则返回False。
字符串isdecimal
print(LokeshGupta.isdecimal()) # Falseprint(12345.isdecimal()) # Trueprint(123.45.isdecimal()) # False - Contains pointprint(1234 5678.isdecimal()) # False - Contains space6.15. isdigit()
True如果所有字符都是数字则返回否则返回False。指数也被认为是数字。
字符串isdigit
print(LokeshGupta.isdigit()) # Falseprint(12345.isdigit()) # Trueprint(123.45.isdigit()) # False - contains decimal pointprint(1234\u00B2.isdigit()) # True - unicode for square 26.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 spaces6.17. islower()
True如果所有字符均小写则返回否则返回False。不检查数字符号和空格仅检查字母字符。
字符串islower
print( LokeshGupta.islower() ) # Falseprint( lokeshgupta.islower() ) # Trueprint( lokesh_gupta.islower() ) # Trueprint( lokesh_gupta_38.islower() ) # True6.18. isnumeric()
True如果所有字符都是数字0-9则it方法返回否则返回False。指数也被认为是数值。
字符串isnumeric
print(LokeshGupta.isnumeric()) # Falseprint(12345.isnumeric()) # Trueprint(123.45.isnumeric()) # False - contains decimal pointprint(1234\u00B2.isnumeric()) # True - unicode for square 26.19. isprintable()
它返回True如果所有字符都打印否则返回False。不可打印字符用于指示某些格式化操作例如
空白被视为不可见的图形回车标签换行分页符空字符
字符串isprintable
print(LokeshGupta.isprintable()) # Trueprint(Lokesh Gupta.isprintable()) # Trueprint(Lokesh\tGupta.isprintable()) # False6.20. isspace()
True如果字符串中的所有字符都是空格则返回否则返回False。
6.21. istitle()
它返回True如果文本的所有单词以大写字母开头字的其余均为小写字母即标题案例。否则False。
字符串istitle
print(Lokesh Gupta.istitle()) # Trueprint(Lokesh gupta.istitle()) # False6.22. isupper()
True如果所有字符均大写则返回否则返回False。不检查数字符号和空格仅检查字母字符。
字符串isupper
print(LOKESHGUPTA.isupper()) # Trueprint(LOKESH GUPTA.isupper()) # Trueprint(Lokesh Gupta.isupper()) # False6.23. join()
它以可迭代方式获取所有项目并使用强制性指定的分隔符将它们连接为一个字符串。
字符串join
myTuple (Lokesh, Gupta, 38)x #.join(myTuple)print(x) # Lokesh#Gupta#386.24. ljust()
此方法将使用指定的字符默认为空格作为填充字符使字符串左对齐。
字符串ljust
txt lokeshx txt.ljust(20, -)print(x) # lokesh--------------6.25. lower()
它返回一个字符串其中所有字符均为小写。符号和数字将被忽略。
字符串lower
txt Lokesh Guptax txt.lower()print(x) # lokesh gupta6.26. lstrip()
它删除所有前导字符默认为空格。
字符串lstrip
txt #Lokesh Guptax txt.lstrip(#_,.)print(x) # Lokesh Gupta6.27. maketrans()
它创建一个字符到其转换/替换的一对一映射。当在translate()方法中使用时此翻译映射随后用于将字符替换为其映射的字符。
字符串maketrans
dict {a: 123, b: 456, c: 789}string abcprint(string.maketrans(dict)) # {97: 123, 98: 456, 99: 789}6.28. partition()
它在给定的文本中搜索指定的字符串并将该字符串拆分为包含三个元素的元组
第一个元素包含指定字符串之前的部分。第二个元素包含指定的字符串。第三个元素包含字符串后面的部分。
字符串partition
txt my name is lokesh guptax txt.partition(lokesh)print(x) # (my name is , lokesh, gupta)print(x[0]) # my name isprint(x[1]) # lokeshprint(x[2]) # gupta6.29. replace()
它将指定的短语替换为另一个指定的短语。它有两种形式
string.replace(oldvalue, newvalue)string.replace(oldvalue, newvalue, count)–“计数”指定要替换的出现次数。默认为所有事件。
字符串replace
txt A A A A Ax txt.replace(A, B)print(x) # B B B B Bx txt.replace(A, B, 2)print(x) # B B A A A6.30. rfind()
它查找指定值的最后一次出现。-1如果在给定的文本中找不到该值则返回该值。
字符串rfind
txt my name is lokesh guptax txt.rfind(lokesh) print(x) # 11x txt.rfind(amit) print(x) # -16.31. rindex()
它查找指定值的最后一次出现如果找不到该值则引发异常。
字符串rindex
txt my name is lokesh guptax txt.rindex(lokesh) print(x) # 11x txt.rindex(amit) # ValueError: substring not found6.32. rjust()
它将使用指定的字符默认为空格作为填充字符来右对齐字符串。
字符串rjust
txt lokeshx txt.rjust(20,#)print(x, is my name) # ##############lokesh is my name6.33. rpartition()
它搜索指定字符串的最后一次出现并将该字符串拆分为包含三个元素的元组。
第一个元素包含指定字符串之前的部分。第二个元素包含指定的字符串。第三个元素包含字符串后面的部分。
字符串rpartition
txt my name is lokesh guptax txt.rpartition(lokesh)print(x) # (my name is , lokesh, gupta)print(x[0]) # my name isprint(x[1]) # lokeshprint(x[2]) # gupta6.34. rsplit()
它将字符串从右开始拆分为一个列表。
字符串rsplit
txt apple, banana, cherryx txt.rsplit(, )print(x) # [apple, banana, cherry]6.35. rstrip()
它删除所有结尾字符字符串末尾的字符空格是默认的结尾字符。
字符串rstrip
txt lokesh x txt.rstrip()print(x) # lokesh6.36. split()
它将字符串拆分为列表。您可以指定分隔符。默认分隔符为空格。
字符串split
txt my name is lokeshx txt.split()print(x) # [my, name, is, lokesh]6.37. splitlines()
通过在换行符处进行拆分它将字符串拆分为列表。
字符串splitlines
txt my name\nis lokeshx txt.splitlines()print(x) # [my name, is lokesh]6.38. startswith()
True如果字符串以指定值开头则返回否则返回False。字符串比较区分大小写。
字符串startswith
txt my name is lokeshprint( txt.startswith(my) ) # Trueprint( txt.startswith(My) ) # False6.39. strip()
它将删除所有前导开头的空格和结尾结尾的空格字符默认为空格。
字符串strip
txt my name is lokesh print( txt.strip() ) # my name is lokesh6.40. swapcase()
它返回一个字符串其中所有大写字母均为小写字母反之亦然。
字符串swapcase
txt My Name Is Lokesh Guptaprint( txt.swapcase() ) # mY nAME iS lOKESH gUPTA6.41. title()
它返回一个字符串其中每个单词的第一个字符均为大写。如果单词开头包含数字或符号则其后的第一个字母将转换为大写字母。
字符串标题
print( lokesh gupta.title() ) # Lokesh Guptaprint( 38lokesh gupta.title() ) # 38Lokesh Guptaprint( 1. lokesh gupta.title() ) # Lokesh Gupta6.42. translate()
它需要转换表来根据映射表替换/翻译给定字符串中的字符。
字符串translate
translation {97: None, 98: None, 99: 105}string abcdef print( string.translate(translation) ) # idef6.43. upper()
它返回一个字符串其中所有字符均为大写。符号和数字将被忽略。
字符串upper
txt lokesh guptaprint( txt.upper() ) # LOKESH GUPTA6.44. zfill()
它在字符串的开头添加零0直到达到指定的长度。
字符串zfill
txt 100x txt.zfill(10)print( 0000000100 ) # 0000000100
祝学习愉快、工作顺利
关注公众号「码农园区」获取程序员大礼包