Use of strings in Python

Source: Internet
Author: User

Representation of string strings in Python

The string has a total of 4 representation methods

    1. Use a pair of single quotes to denote
      ' Python '

    2. Use a pair of double quotation marks to denote
      " Python "

    3. Use a pair of three single quotes or a pair of three double quotation marks to represent multiple lines of string
      "" " 

    4. Use () to nest strings with quotation marks
      ' here is a double quote (") ' " here's a single quote (') "

  

Use of strings
    • Index operation--Returns a single character in a string
" python " [0]
    • Slice operation--returns a substring from M to n (n) index not included
" Python " [1:3]
    • Advanced Slice operation-Returns a slice of k from M to N, with the M default beginning, and n the default value ending
" 0 1,234,567,890 " " 1357 " " 0 1,234,567,890 " " 9,876,543,220 "
The operator of the string
    • X+y

Connect two strings x and Y

    • N*x or X*n

Copy n times x string

    • X in S

Returns True if X is a substring of s, otherwise false.

string-related functions
    • Len (x)

Returns the length of the string x

    • STR (x)

Returns the string form of any type X

    • Hex (x)

16 binary form of integer x

    • Oct (x)

8 binary form of integer x

    • Chr (x)

X is a Unicode encoding that returns its corresponding characters.

    • Ord (x)

X is the corresponding character and returns its corresponding Unicode encoding.

Common methods of strings
    • Str.lower ()

Returns a copy of the string, with all characters lowercase.

    • Str.upper ()

Returns a copy of the string, with all characters capitalized.

    • Str.split (Sep=none)

Returns a list of sections in Str that are separated by Sep.

    • Str.count (sub)

Returns the number of times a sub substring appears in Str.

    • Str.replace (old,new)

Returns a copy of the string str, and all old substrings are replaced with new.

    • Str.center (Width,[,fillchar])

The string is centered on width widths. Fill word defaults think English space.

    • Str.strip (chars)

Remove all characters from the chars string from the left and right sides of Str.

    • Str.join (ITER)

Adds a str after each character in the string ITER except for the last character. Commonly used to divide words using.

Use of strings in Python

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.