In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article focuses on "how to master python strings". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to master python strings.
Four prefixes
In addition to ordinary strings, python can be preceded by four prefixes, frub. Where the f string can convert the variables in {} into strings; r means to cancel the escape; u means to use Unicode characters; b means to use the byte type.
The first two are the most commonly used. Examples are as follows
> f "1cm 1 = {1mm 1}" # f string'1x 1m 2' > r "C:\ abc\ def" # r string unescaped'C:\ abc\\ def' uppercase and lowercase conversion instructions sample results capitalize () initials to capitalize'I love u'.capitalize ()'I love u'upper () all letters to capitalization'I love u'.upper ()'I LOVE U'lower () Lowercase'I LOVE U'.lower ()'i love u'title () initials'I love u'.title ()'I Love U'swapcase () uppercase flip'I Love U'.swapcase ()'i lOVE u'casefold () Super uppercase to lowercase'Γ '.casefold ()' γ 'space adjustment function
Enter w to adjust the character length to w, and if not specified, the rest of the positions are supplemented with spaces.
Space adjustment explains the example result center (w) adjusted original string centered 'Love'.center (6)' Love' ljust (w) adjusted original string left 'Love'.ljust (6)' Love' rjust (w) adjusted original string right 'Love'.rjust (6)' Love'zfill (w) complement 0'123'.zfill (6) '000123'expandtabs (w) on the left to convert tab to w spaces'\ ta'.expandtabs (4)'a' to delete some characters
S is a string and defaults to a space if it is empty.
Lstrip (s) deletes the character 'ILoveU'.lstrip (' ILU') 'oveU'rstrip (s) in s from the right, deletes the character'\ tLove\ t'.rstrip ()'\ tLove'strip (s) in s, executes lstrip (s) and rstrip (s)'\ tLove\ t'.strip () 'Love'removeprefix (s) deletes s'ILoveU'.removeprefix (' ILU') 'ILoveU'removesuffix (s) from the left, deletes s'ILoveU'.removesuffix (' eU') 'ILov' string from the right
Return True conditional example return value isalnum () contains only letters or numbers' abc1'.isalnum () Trueisalpha () contains letters only 'abc1'.isalpha () Falseisdecimal () contains only decimal numbers' 123A'.isdecimal () Falseisdigit () contains only numbers' 123A'.isdecimal () Falseislower () contains letters, and all lowercase 'abc1'.islower () Trueisupper () contains letters And all uppercase '123'.isupper () Falseisnumeric () contains only the numeric character' four five '.isnumeric () Trueisspace () contains only spaces' asd'.isspace () Falseistitle () both uppercase'I Love U'.istitle () Trueisascii () are ASCII codes' four'. Isascii () Falseisidentifier () can be used as a python identifier '1asd'.isidentifier () Falseisprintable () is a printable character chr (1). Isprintable () False string lookup
The following functions all have three input parameters, str for the string to be matched, beg and end for the beginning and end, respectively, and the default is 0 and the length of the matched string. Take s.count (str, beg, end) as an example to show the number of str occurrences in s [beg:end].
Number of occurrences of s.count (str, beg, end) str s.find (str, beg, end) the location where s.find (str, beg, end) str first appeared. If not found, return the location where-1s.rfind (str, beg, end) str last appeared. If not found, return-1s.index (str, beg, end) is the same as find, but if not found, s.rindex (str, beg, end) is the same as rfind, but if not found, s.startswith (str, beg, end) will be returned if it starts with str. Returns Trues.endswith (str, beg, end) if it ends with str, returns True to split, merge and replace s.split (str, num) to split s according to str. If num is specified, it is divided into num+1 segments s.rsplit (str, num) and split, but matches s.splitlines ([keepends]) by line from right to left. If keepends is False, the newline character s.join (seq) is not retained. Combine strings in seq s.partition (str) divides s into three parts, str left, str and str right s.rpartition (str) is the same as partition, but starting from the right s.replace (S1, S2, num) replaces S1 in s with S2, if num is specified, the replacement does not exceed Num times
In addition to replace, you can also replace it through translate, except that translate inputs the replacement table established by maketrans.
> trans =''.maketrans (' I', "I") > "I Love U". Translate (trans)'I Love U'format format
Format replaces the identifier {} with the value in the tuple, or sequentially if the sequence number is not specified in {}.
'{} Love {}' .format ('I Love U')'I Love U'
If a sequence number is specified in {}, it is replaced in the order of the sequence number
>'{0} Love {1}, {1} Love {0} '.format (' I'm not in charge of U')'I Love U, U Love I'# has recently been brainwashed by Honey Snow Ice City.
Of course, like the C language, this format is more often used for digital conversion. Python declares the format after the number is converted to a string by:
Among them
^, which represents center, left alignment, and right alignment, respectively
+ means to display + before a positive number and-before a negative number.
B, d, o and x are binary, decimal, octal and hexadecimal, respectively
Specific examples are as follows
> from math import pi input and output description'{: .2f} '.format (pi)' 3.14 'retains two decimal places, rounding' {: .2%} '.format (pi)' 314.16% 'retains the percentage of two decimal places' {: + .2f} '.format (1)' + 1.00' retains two decimal places, with the sign'{: 0 > 5d} '.format (1)' 00001' added to the left. The width is 5'{: > 5d} '.format (1)' 01' with a width of 5'{: X
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.