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

What is the basis of Python string

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

Share

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

This article shows you what is the basis of the Python string, the content is concise and easy to understand, it will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

1. Constant

Single quotation mark: 'spa''m'

Double quotes: "spa'm"

Three quotation marks:''. Spam...''', "". Spam. "

Escape character: "s\ tp\ na\ 0m"

Raw string: r "C:\ new\ test.spm"

Byte string: b'sp\ x01am'

Unicode string "u'eggs\ u0020spam'

Escape sequences represent special characters

String backslash character

Escape meaning\ newline ignores (continuous)\\ backslash (reserved\)\ "double quotation marks (reserved")\ "double quotation marks (reserved")\ a bell\ b backwards\ nnewline\ r returns\ t horizontal tabs\ v vertical tabs\ N {id} Unicode database ID\ uhhhhUnicode 16-bit hexadecimal values\ UhhhhhhhhhUnicode 32-bit hexadecimal values a\ xhh hexadecimal values\ ooooco The binary value\ ONull (not the end of the string)\ other is not escaped (reserved). Raw string suppresses escape

Contrast result

Print ('C:\ new\ text.dat') print (ringing C:\ new\ text.dat') IV, Unicode string print (u'Hello World!') Write the multiline string a='Always look\ non the bright\ nside of life'b='''Always lookon the brightside of life'''print (a) print (b) VI. String operator

The value of the instance variable an in the following table is the string "Hello" and the value of variable b is "Python":

Operator description instance + string concatenation

A + b

'HelloPython'

* repeat output string

A * 2

'HelloHello'

[] get characters in a string by index

A [1]

'e'

[:] intercept part of a string

A [1:4]

'ell'

In member operator-returns True if the string contains a given character

"H" in a

True

Not in member operator-returns True if the string does not contain the given character

"M" not in a

True

Razar R original string-original string: all strings are used directly literally, without escaping special or unprintable characters. The original string has almost exactly the same syntax as a normal string, except for the letter "r" (which can be uppercase and lowercase) before the first quotation mark of the string.

Print (r'\ n')

\ n

Print (R'\ n')

\ n

Seven: string formatting symbol description% c formatting character and its ASCII code% s formatting string% d formatting integer% u formatting unsigned integer% o formatting unsigned octal number% x formatting unsigned hexadecimal number% X formatting unsigned hexadecimal number (uppercase)% f formatting floating point number, you can specify precision% e after decimal point% e formatting floating point number% E with scientific counting method Using scientific counting to format the abbreviations of floating point numbers% g% f and% e% G% f and% E the abbreviation% p uses hexadecimal numbers to format the address of the variable 8, the string built-in function method describes string.capitalize (), returns the first character of the string in uppercase string.center (width) to the center of the original string, and fills in spaces to the new string string.count (str, beg=0) of length width End=len (string) returns the number of times str appears in string If beg or end is specified, the number of occurrences of str in the specified range is returned string.decode (encoding='UTF-8', errors='strict') decodes string in the encoding format specified by encoding, and if an error occurs, an exception of ValueError is reported by default, unless errors specifies' ignore' or 'replace'string.encode (encoding='UTF-8', errors='strict') encodes string in the encoding format specified by encoding If an error occurs, a ValueError exception is reported by default, unless errors specifies' ignore' or 'replace'string.endswith (obj, beg=0, end=len (string)) to check whether the string ends with obj, and if beg or end is specified, check whether the specified range ends with obj. If so, return True, otherwise return False.string.expandtabs (tabsize=8) to convert the tab symbol in the string string to spaces. The default number of tab spaces is 8. String.find (str, beg=0, end=len (string)) detects whether str is included in string. If beg and end specify a range, check whether it is included in the specified range. If it returns the starting index value, otherwise return-1string.format () format string string.index (str, beg=0, end=len (string)) is the same as find () method. Only if str does not report an exception in string. String.isalnum () returns True if string has at least one character and all characters are letters or numbers, otherwise returns Falsestring.isalpha () returns True if string has at least one character and all characters are letters Otherwise return Falsestring.isdecimal () if string contains only decimal numbers return True otherwise return False.string.isdigit () if string contains only numbers return True otherwise return False.string.islower () if string contains at least one case-sensitive character And all these (case-sensitive) characters are lowercase, True is returned, otherwise Falsestring.isnumeric () is returned if the string contains only numeric characters, True is returned, otherwise Falsestring.isspace () is returned if the string contains only spaces, True is returned, otherwise False.string.istitle () is returned if the string is themed (see title ()) Otherwise, return Falsestring.isupper () if the string contains at least one case-sensitive character, and all these (case-sensitive) characters are uppercase, return True, otherwise return Falsestring.join (seq) with string as the delimiter Merge all the elements in seq into a new string string.ljust (width) returns a new string left-aligned and uses spaces to fill the new string string.lower () of length width to convert all uppercase characters in string to lowercase .string.lstrip () truncate the space to the left of string string.maketrans (intab, outtab]) maketrans () method is used to create a translation table for character mapping For the simplest way to accept two parameters, the first parameter is a string, which represents the character to be converted, and the second parameter is the string that represents the target of the conversion. Max (str) returns the largest letter in the string str. Min (str) returns the smallest letter in the string str. String.partition (str) is a bit like a combination of find () and split (). From the first position where str appears, the string string is divided into a ternary tuple (string_pre_str,str,string_post_str). If the string does not contain str, then string_pre_str = = string.string.replace (str1, str2, num=string.count (str1)) replace str1 in string with str2, if num specifies The replacement is no more than num times. String.rfind (str, beg=0,end=len (string)) is similar to the find () function, but looks up .string.rindex (str, beg=0,end=len (string)) is similar to index () The new string string.rpartition (str) is similar to the partition () function, but starts from the right to look for .string.rstrip () to remove the space at the end of the string string. String.split ("", num=string.count (str)) slices the string with str as the delimiter, if num has a specified value. Then only the num substrings string.splitlines ([keepends]) are separated by lines ('\ r','\ r\ n',\ n'), returning a list of lines as elements. If the parameter keepends is False, there is no newline character, if it is True, the newline character is retained. String.startswith (obj, beg=0,end=len (string)) checks whether the string begins with obj, returns True if it is, and returns False otherwise. If beg and end specify values, check .string.strip ([obj]) to perform lstrip () and rstrip () string.swapcase () flip uppercase string.title () in string on the specified range. String.title () returns the "titled" string, meaning that all words start in uppercase and the rest are lowercase (see istitle ()) strip (str, del= "") to convert string characters according to the table given by str (containing 256characters). The characters to be filtered are put in the del parameter string.upper () converts lowercase letters in string to uppercase string.zfill (width) returns a string of length width, the original string string is right-aligned, and the 0string.isdecimal () isdecimal () method is filled in front to check whether the string contains only decimal characters. This method exists only in unicode objects. The above content is what is the basis of the Python string, have you learned the knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

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