python learning II, common string operations

String can be said to be the most widely used in daily development. Now let's summarize some common operations related to string in python

First we declare a string variable

1 str = "hello world"

Let's introduce each method in turn

print(str.capitalize()) #String initial uppercase
print(str.casefold()) #String all characters lowercase
print(,'-')) #Place the string at the center of the second parameter. Make sure that the first number parameter is the modified length of the entire string

---------hello world----------
print(str.count("l",0,5))#Count the occurrence times of a character in the string. The second parameter is the start search position and the third parameter is the end search position. At this time, the print data is 2

str2 = "Hello world"
print(str2.encode("GBK")) #Encodes the string in the specified format, default to utf-8
print(str.endswith("d")) #Judge whether to end with parameter character
print(str.find("l",0,10)) #Query the index value of the first occurrence of this character within the index range, and the print value is 2
print(str.index("l",1,10)) #Query the index value of the first occurrence of this character within the index range, and the print value is 2,

Say the difference between index() and find(). If index cannot find the index of the character in the range, it will report an error, and find will return - 1 without an error


print(str.isalnum()) #Checks whether a string consists of letters and numbers,Printing false,Because there are spaces in the string, spaces are also detected
print(str.isalpha()) #Check whether the string is only composed of letters, print false,Because there are spaces in the string, spaces are also detected
print(str.isdecimal()) #Check if the string contains only decimal characters
print(str.isdigit()) #Check whether the string is composed of numbers only
print(str.islower()) #Check whether the string is composed of lowercase letters
print(str.isnumeric()) #Check whether the string is composed of numbers only,And sidigit I don't know the difference
print(str.isspace()) #Check whether the string consists of only white space characters
print(str.istitle()) #Check whether the initial of all words in the string is uppercase and other letters are lowercase
print(str.isupper()) #Checks whether all letters in a string are uppercase


#join The method is to link a string sequence, such as a list or ancestor, with some characters
str3 = ["h","e","l","l","o"];
str4 = ("w","o","r","l","d")

#Print as follows
print(str.ljust(20,"_")) #Returns the left alignment of the original string,And fills the new string with spaces to the specified length. If the specified length is less than the length of the original string

hello world_________
print(str.lstrip("h")) #Used to truncate spaces or specified characters to the left of a string

ello world
print(str.replace("l","a")) #The first parameter is the old character, the second parameter is the new tariff to be replaced, and the third parameter is the maximum number of times to replace, which can be left blank

heaao worad

print(str.replace("l","a",1)) #If you add a third parameter, such as replace only once

healo world
print(str.split(" ")) #Slice: cut the string into a list according to the characters in the parameter. The second parameter can specify the slice times

['hello', 'world']

print(str.rsplit("l",1)) #Slice, and split The difference is that you start with a throne

['hello wor', 'd']
print(str.startswith("h")) #Check if it starts with a custom string

print(str.strip()) #Remove characters specified at the beginning and end of a string,Space by default

print(str.swapcase()) #Convert string case

print(str.title()) #Capitalize the beginning of each word in the string and lowercase the rest

print(str.upper()) #Convert lowercase letters to uppercase

print(str.zfill()) #Returns a string of a specified length. The original string is right justified and filled with 0

Tags: Python less REST

Posted on Sun, 05 Apr 2020 18:05:27 -0400 by mtb211