Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Python string method

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces the "Python string method" related knowledge, in the actual case operation process, many people will encounter such a dilemma, and then let the editor lead you to learn how to deal with these situations! I hope you can read it carefully and be able to achieve something!

Format the string split ()

Split a string into a list, with spaces as delimiters by default

A = "you can't see mee" a.split () # output content is ["you", "can't", "see", "me"] a.split ("'") # output content is ['you cant','t see me'] strip ()

Used to remove characters from both ends of the string; when the parentheses are blank, the white space character is deleted by default (including'\ nblank,'\ ringing,'\ tasking,'')

A = "123" a.strip () # the output is" 123". Note that there is a space before it.

Example 2:

A = "0000000this is string example.wowning examples 0000000" print a.strip ("0") # the output result of the above instance is as follows: this is string example.wowning examples!

Special note: as long as the deleted content exists, regardless of the positive or negative order, such as strip ("12") and strip ("21"), as shown below

C = "123acb" c.strip ("12") # output content is "3abc" c.strip ("21") # output content is also "3abc" strip () summary:

The strip function of python can be used in two ways: generally go to the beginning and end

If the parameter is omitted, the removal of spaces at both ends will be performed. Such as:

Str= "abc" print (str.strip ()) # result is abc

If a parameter is passed in, the corresponding characters are removed at both ends according to the characters, but at this time it has nothing to do with spaces.

Str= "abc" print (str.strip ("a")) # output "bc" print (str.strip ("ac")) # output "abc" did nothing print (str.strip ("a")) # output "abc" did nothing join ()

Link elements in strings, lists, dictionaries, and tuples into new strings

A = "123" |" .join (a) > >'1 | 2 | 3join (b) >'a bcjoin (b) > >'a b / c = ('iLiangliao') "_" .join (c) > > 'i_j_k's = {"name": "lee", "age": 18} "_" .join (s) >' name_age'

Note: the contents of lists, tuples and other sequences must be strings, otherwise an error will be reported.

Replace (old, new, number of substitutions)

String substitution, the first parameter old string, the second string to replace, the third number of substitutions, can be empty default to replace all

S = "hello python python python" print (s.replace ("python", "java")) print (s.replace ("python", "java", 2)) # output hello java java javahello java java pythonfind ()

Check whether the string contains a substring str lookup content in the first character, there is no return * *-1 contains * *

A = "you can't see me" a.find ("you") # output content is 0afind ("can't") # output content is 4a.find ("asd") # output content is-1index ()

Detect whether the string contains the substring str

The usage is similar to find (), but if the search does not exist, an error is returned. If you specify the beg (start) and end (end) range, check whether it is included in the specified range.

A = "you can't see me" a.index ("you") # output content is 0a.index ("can't") # output content is 4a.index ("asd") # output content is: Traceback (most recent call last): File ", line 1 focus in ValueError: substring not parameters ```what should I do if the parameter appears many times? Example 2: ``t = tuple ('Allen') print (t) # output (' Aids, 'lags,' lags, 'estranges,' n') a=t.index ('lame journal 2) print (a) # output 2```Because the location of the first' l' is 1, so we will start indexing plus 1 to continue searching, and sure enough, we will find'l' at the location where the index is 2. Seek ()

The seek () function is a function that belongs to a file operation and is used to move the file read pointer to a specified location.

Syntax:

FileObject.seek (offset [, whence]) # offset-the starting offset, which represents the number of bytes to move the offset # whence: optional, default is 0. Give the offset parameter a definition, indicating where to start the offset; 0 means from the beginning of the file #, 1 from the current location, and 2 from the end of the file. Upper ()

Convert to uppercase

S='abc's.upper () # output ABClower ()

Convert to lowercase

Swapcase ()

Convert capital letters to lowercase, lowercase letters to uppercase

Capitalize ()

Convert the first character in a string to uppercase and the rest to lowercase

Title ()

Change the first letter of each word in a string to uppercase

Note: the above method does not change the original string, but produces a new string

Case list:

S = "heLlO World" a=s.swapcase () b=s.capitalize () c=s.title () d = s.upper () e = s.lower () print ("swapcase:", a) print ("capitalize:", b) print ("title:", c) print ("upper:", d) print ("lower:" E) print (s.isalpha ()) print (s) # output swapcase: HElLo wORLD capitalize: Hello worldtitle: Hello World upper: HELLO WORLD lower: hello world False # Note that spaces do not count as letters heLlO Worldisalpha ()

Whether it is all letters, note: if there is a space, it is not a letter, but a Chinese string is a letter.

S = "heLlO World" print (s.isalpha ()) S1 = "hello" print (s1.isalpha ()) print ("Zhang San" .isalpha ()) print ("Zhang San 1" .isalpha ()) # output FalseTrueTrueFalseisnumeric ()

Is it all made up of numbers?

Note: Chinese numerals, Roman numerals, string numbers, and special symbols in the input method that are counted as numbers.

English numbers don't count.

Print ("1234" .isnumeric () print ("one two 34" .isnumeric ()) print ("one two" .isnumeric ()) print ("one" .isnumeric ()) # is not print ("ⅠⅡⅢⅣ" .isnumeric ()) print ("㈠" .isnumeric ()) # output TrueTrue True FalseTrue True

Special digital symbol

Print ("❶" .isnumeric () print ("①" .isnumeric ()) print ("⒈" .isnumeric ()) # Note here is not a point, the number print ("⒒" .isnumeric ()) print ("⑴" .isnumeric ()) print ("⑾" .isnumeric ()) TrueTrueTrueTrueTrueTrue in the special symbol of the input method

Isalnum ()

Is it all alphanumeric?

Note: the special symbols in the input method calculate numbers, and common punctuation marks are not alphanumeric! Etc.

That's all for "Python string method". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report