Detailed Explanation of Python String
- 2021-12-13 08:44:56
- OfStack
Directory 1. Partial escape characters 2. slice slice read string 3. Call split () method to split string ASCII letter 4. Method related to letter case 5. Search lookup string summary
Summarize
1. Partial escape characters
Escape character
# \\ Backslash
str1 = "qqq\\qq"
print(str1)
# Output qqq/qq
# \b Backspace key ( Backspace )
str2 = "qqq\b"
print(str2)
# Output qq
# \' Single quotation mark \" Double quotation marks
str3 = "qq\'qqqqq\""
print(str3)
# Output qq'qqqqq"
# \n Line break
str4 = "qqqq\nqq"
print(str4)
# Output qqqq
# qq
# \t Tabs ( Tab )
str5 = "a\taa"
print(str5)
# Output a aa
2. slice slice read string
s = "hello world sssss sssss sssss"
# s[n] Specifies that the subscript reads an element in the sequence
print(s[1])
# e
# s[n: m] From subscript value n Read to m-1 , several elements
print(s[0: 4])
# hell
# s[n:] From subscript value n Read to the end 1 Elements
print(s[3:])
# lo world
# s[:m] From subscript value 0 Read to m-1 Elements
print(s[:5])
# hello
# s[:] Indicates that it will be copied 1 Elements of a partial sequence
print(s[:])
# hello world
# s[::-1] Invert the whole sequence element
print(s[::-1])
# dlrow olleh
3. Call the split () method to split the string ASCII letter
# String .split( Delimiter, number of separations)
# Output 26 Lowercase letters and invert the output
letters = ""
for x in range(97, 123):
letters += str(chr(x))
print(letters)
print(" ")
print(letters[::-1])
# ord() Returns the corresponding character ASCII Code
# chr() Return ASCII Characters corresponding to code
# Output 26 Uppercase letters and invert the output A 65 Z 91
letters2 = ""
for n in range(65, 91):
letters2 += chr(n) + " "
print(letters2)
print(letters2[::-1].split(" ",5)) # String .split( Delimiter, number of separations)
4. Methods related to letter case
str = "My name in Zyj hello world"
# capitalize() Only the first 1 The first letter of the word is capitalized and the rest is lowercase
print(str.capitalize())
# My name in zyj hello world
# lower() Convert letters to lowercase
print(str.lower())
# my name in zyj hello world
# upper() Convert letters to uppercase
print(str.upper())
# MY NAME IN ZYJ HELLO WORLD
# title() The first letter of each word is capitalized and the rest is lowercase
print(str.title())
# My Name In Zyj Hello World
# islower() isupper() istitle() Determine whether the string conforms to the format
print(str.isupper())
# False
5. Search for lookup strings
str1 = "Myaa namess inddaa Zyjcc helloxx worldbb"
# 1.count.py Search for the number of specific strings that exist
print(str1.count("aa"))
# 2. Lookup string str.find( Character or string Beginning subscript , End subscript ) Return to the 1 The subscript number the next time the string is found
# find The () method returns when it does not find a substring -1
str2 = "My name in Zyj hello world My name in Zyj hello world"
print(str2.find("in", )) # Looking for substrings in , numbered from the subscript 0 Begin
print(str2.find("in", 9)) # Looking for substrings in , numbered from the subscript 9 Begin
# 3. str.index( Character or string Beginning subscript , End subscript ) Returns the subscript value of the specified string
print(str2.index("name"))
# index And find Difference, index () If you can't find it, you will report an error. find () returns -1 Value
# 4.startswith( Character or string Beginning subscript , End subscript ) Determine whether the beginning character of a string contains subcharacters
str3 = "My name in Zyj hello world My name in Zyj hello world"
print(str3.startswith("name", 3)) # True
# 5.str.endswith( Character or string Beginning subscript , End subscript ) Determine whether the character at the end of the string contains subcharacters
print(str3.endswith("world")) # True
Summarize
This article is here, I hope to give you help, but also hope that you can pay more attention to this site more content!