[Python extension Reading] string method and comment, python string

Source: Internet
Author: User

[Python extension] string method and comment, python string

Capitalize () Change the first character of the string to uppercase.
Casefold () Change all characters of the entire string to lowercase letters.
Center (width) Center the string and fill it with spaces to the new string with the length and width.
Count (sub [, start [, end]) Returns the number of times that sub appears in the string. the start and end parameters indicate the range. Optional.
Encode (encoding = 'utf-8', errors = 'strict ') Encode the string in the encoding format specified by encoding.
Endswith (sub [, start [, end]) Check whether the string ends with a sub-string. If yes, return True; otherwise, return False. The start and end parameters are optional.
Expandtabs ([tabsize = 8]) Convert the tab character (\ t) in the string to a space. If no parameter is specified, the default number of spaces is tabsize = 8.
Find (sub [, start [, end]) Checks whether sub is contained in a string. If yes, the index value is returned. Otherwise, the-1, start, and end parameters are returned, indicating the range. Optional.
Index (sub [, start [, end]) Similar to the find method, however, if sub is not in string, an exception is generated.
Isalnum () Returns True if the string contains at least one character and all characters are letters or numbers. Otherwise, returns False.
Isalpha () Returns True if the string contains at least one character and all characters are letters; otherwise, returns False.
Isdecimal () Returns True if the string contains only decimal digits; otherwise, returns False.
Isdigit () Returns True if the string contains only numbers; otherwise, returns False.
Islower () If the string contains at least one case-sensitive character and all these characters are in lower case, True is returned; otherwise, False is returned.
Isnumeric () If the string contains only numeric characters, True is returned; otherwise, False is returned.
Isspace () If the string contains only spaces, True is returned; otherwise, False is returned.
Istitle () True is returned if the string is title (all words start with uppercase and all other letters are lowercase). Otherwise, False is returned.
Isupper () If the string contains at least one case-sensitive character and all these characters are uppercase, True is returned; otherwise, False is returned.
Join (sub) Use a string as the separator and insert it into all the characters of sub.
Ljust (width) Returns a left-aligned string and fills it with spaces to a new string with a width length.
Lower () All uppercase characters in the conversion string are lowercase.
Lstrip () Removes all spaces on the left of the string.
Partition (sub) Locate the sub-string and divide the string into three tuples (pre_sub, sub, fol_sub). If the sub is not included in the string, return ('original string ','','')
Replace (old, new [, count]) Replace the old substring in the string with the new substring. If count is specified, it cannot exceed count.
Rfind (sub [, start [, end]) It is similar to the find () method, but it is searched from the right.
Rindex (sub [, start [, end]) It is similar to the index () method, but starts from the right side.
Must ust (width) Returns a right-aligned string and fills it with spaces to a new string with a width length.
Rpartition (sub) Similar to the partition () method, but the query starts from the right side.
Rstrip () Removes spaces at the end of a string.
Split (sep = None, maxsplit =-1) By default, the non-parameter slice string is separated by spaces. If the maxsplit parameter is set, only the maxsplit substrings are separated and the list of substrings after the slice is returned.
Splitlines ([keepends]) Separated by '\ n', a list containing all rows as elements is returned. If the keepends parameter is specified, the former keepends row is returned.
Startswith (prefix [, start [, end]) Check whether the string starts with prefix. If yes, True is returned. Otherwise, False is returned. Range check can be specified for the start and end parameters. Optional.
Strip ([chars]) Delete all leading and trailing spaces of a string. The chars parameter can be used to customize the characters to be deleted. Optional.
Swapcase () Flip the case in the string.
Title () Returns a title string (all words start with uppercase and all other letters are lowercase.
Translate (table) Convert characters in a string according to table rules (which can be customized by str. maketrans ('A', 'B.
Upper () All lowercase characters in the conversion string are in uppercase.
Zfill (width) Returns a string with the length of width. The original string is right aligned and the front side is filled with 0.

Http://bbs.fishc.com/forum.php? Mod = viewthread & tid = 38992.


Conversion of strings and arrays in Python

['X', 'y', 'z'] => 'xyz'
B = ''. join (['x', 'y', 'z'])
'Xyz' => ['x', 'y', 'z']
A = list ('xyz ')

String operation python String Conversion to list

#! /Usr/bin/python
# Encoding: UTF-8

Str = "['mac = $ AUTOGEN_MAC, bridge = xenbr1, model = pcnet', 'mac = 00: 16: 3e: 4b: a2: 30, bridge = xenbr0, model = ']"
Lst = [','. join ([('mac = $ autogen_mac' if item [: 4] = 'mac = 'else item)
For item in m. split (',')])
For m in eval (str)]
Print repr (lst)

#~ > Python-u "test. py"
#~ ['Mac = $ AUTOGEN_MAC, bridge = xenbr1, model = pcnet', 'mac = $ AUTOGEN_MAC, bridge = xenbr0, model = ']
#~ > Exit code: 0 Time: 0.074

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.