Python Learning string

Source: Internet
Author: User

1.String type: A finite sequence consisting of 0 or more characters

Note: In Python, double and single quotes have the same meaning and can be used to represent strings. 2. String-built functions and operatorsstrip () remove whitespace, assign value to new variable
"= a.strip ()print(b)

Split () splits the string into a list of multiple strings.

' a b c D '  = a.split ()print(b)

Len () calculates the string length

' a b c D ' Print (Len (a))

index () to find where the element is located

' a b c D ' Print (A.index ('b'))

[start:end:step] slices, getting substrings from a string

' a b c D ' Print (A[0:5:1])

3. String formatting:

string formatting is the connection, substitution, and return of a new compliant string only by the specified rule. the expression syntax for formatting strings in Python is: format_string% String_to_convert or format_string% (string_to_convert1,string_to_convert2,...)
Print ("Hello%s, age:%d" % ("Simon", 24))
formatting of the string format ()
s="Hello {0},age {1}"    #{0},{1} is a placeholder s1=s.format ('  Simon','the ')print(S1)

In addition to formatting, sometimes you need to adjust the format symbol display method, the auxiliary format conforms to meet these requirements.

fixed content in a formatted string can contain non-displayed characters in addition to characters that can be displayed , such as letters, numbers, punctuation, and so on. This character is called an escape character . Disables the escape character with ' R '.

4. String connection join ()

Li = ["a""b""_". Join (LI)print(L1)

5. String separator spilt () and partition ()

difference: Split () passed in parameters, disappeared after segmentation, output as a listpartition () The parameters passed in, the partition exists, the output is a tupleSplitlines () split with newline (\ n) characters
' AXBCD '  = a.partition ('x' = A.split ('x')  Print(b)     #(' A ', ' X ', ' BCD ')print(d)     # [' A ', ' BCD ']

6.id () View the memory address of a string or number

' Simon '  = ID (a)print(b)    #2767282156912

   

Python Learning string

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.