How to analyze python string related str
Today I'll show you how to analyze the str related to python strings. The content of the article is good. Now I would like to share it with you. Friends who feel in need can understand it. I hope it will be helpful to you. Let's read it along with the editor's ideas.
1: visit str single character # for Loop iteration name = 'Chengwei'for ch in name: print (ch End='') # C he n g w e i# Index print (name [1]) # hprint (name [- 1]) # the last one returns the number of str strings for the-1#i#len () function print (len (name)) # 82: string concatenation message = 'hello' +' world'print (message) # helloworld3:str slice message = 'helloworld'print (message [2:4]) # ll4: test words using in and not in String message = 'helloworld'if' hello' in message: print ('YES') # YESif' aaa' not in message: print ('YES') # YES5:str method
String test method
Isalnum () returns true if str contains only letters or numbers and is at least one character long. Otherwise, return falseisalpha () if str contains only letters and is at least one character long, true is returned. Otherwise, return falseisdigit () if str contains only numbers if str contains only letters and is at least one character in length, true is returned. Otherwise, return falseislower ()
If all letters in str are lowercase if str contains only numbers if str contains only letters and is at least one character long, true is returned. Otherwise, return false
Isspace () returns true if str contains only white space characters and is at least one character long. Otherwise, return false (space,\ n,\ t) isupper ()
If all letters in str are uppercase, if str contains only numbers, if str contains only letters and is at least one character long, true is returned. Otherwise, return false
String modification method
Lower () returns a copy of str that converts all letters to lowercase. Invariant lstrip (), which is not a letter, returns a copy of str that removes all leading white space characters. Lstrip (str_item) str_item is a string. Returns deleting all leading STR _ item copies of strings rstrip () returns copies of all trailing white space strings. Rstrip (str_item) returns deleting string copies of all trailing str_item strip () strip (str_item) deleting string copies of all leading and trailing str_item upper () lower () returns str copies that convert all letters to lowercase. Invariant lstrip (), which is not a letter, returns a copy of str that removes all leading white space characters. Lstrip (str_item) str_item is a string. Returns deleting all leading STR _ item copies of strings rstrip () returns copies of all trailing white space strings. Rstrip (str_item) returns deleting string copies of all trailing str_item strip () strip (str_item) deleting string copies of all leading and trailing str_item upper ()
Methods of search and replacement
Endswith (substring) returns true or falsefind (substring) returns the minimum index location where the substring was found. No return-1replace (old, new) returns a copy of the string replacing all old with new starstwith (substring) returns true or false6: repeat operator print ('hello' * 2) # hellohello7: split string message =' hello world chengwei'message_list = message.split () print (message_list) # ['hello',' world', 'chengwei'] message =' hello,world,chengwei'message_list = message.split (' ') print (message_list) # [' hello', 'world',' chengwei'] the above is how to analyze the whole content of the str related to the python string For more information about how to analyze python strings related to str, you can search for previous articles or browse the following articles to learn! I believe the editor will add more knowledge to you. I hope you can support it!