Note five: Python string

Source: Internet
Author: User

A: Learning content

    • String type
    • String type judgment
    • String Type Mutual transfer
    • String Little Exercise

Two: String type

1. basestring

string - related data types in Python are: str and Unicode, both of which are basestring subclasses , str and Unicode are visible as two different types of string objects

2. Byte String type

bytestring= ' Hello world! '

You can see the type of this bytestring is str

3. Unicode string type (plus u in front of normal string)

Unicodestring=u ' Hello Unicode world! '

You can see that the type of this unicodestring is Unicode

Three: String type judgment

1. Determine if it is a string (including STR and Unicode)

#encoding =utf-8

s = "Hello normal string"

u = U ' Hello Unicode '

If Isinstance (s,basestring):

Print U ' is a string '

If Isinstance (u,basestring):

Print U ' is a string '

The result is: either the str string or the Unicode string belongs to the subclass of the basestring class

2. Determine if it is Unicode

#encoding =utf-8

s = "Hello normal string"

u = U ' Hello Unicode '

If Isinstance (S,unicode):

Print S,u ' is Unicode '

If Isinstance (U,unicode):

Print U,u ' is Unicode '

The result of the operation is:

3. Determine if STR is

#encoding =utf-8

s = "Hello normal string"

u = U ' Hello Unicode '

If Isinstance (S,STR):

Print S,u ' is str '

If Isinstance (U,STR):

Print U,u ' is str '

The result of the operation is:

Four: String type mutual transfer

1. Do not specify encoding decoding type for mutual transfer - Use system default encoding

#encoding =utf-8

s= "byte string"

Print type (s)

#str Turn Unicode

U = S.decode ()

Print type (u)

#uncode Turn str

backtobytes = U.encode ()

Print type (backtobytes)

You can see that both Unicode and decode do not specify a name for the codec, and the system's default encoding is used.

2. Specify the encoding decoding type for mutual transfer

#encoding =utf-8

s = "Hello normal string"

Print U "byte string", type (s)

#str Turn Unicode

U = S. Decode ("UTF-8")

Print U "Unicode string", type (U)

#uncode Turn str

backtobytes = U.encode ("UTF-8")

Print U "byte string", type (backtobytes)

The result of the operation is:

Five: String small exercise

1. Small exercise : A string of odd coordinates in the output character string

A = ' gloryroad '

". Join ([a[x] for x in Xrange (Len (a)) if x%2==1])

2. Little exercise Two: Write the string to lowercase, lowercase to uppercase output

s= ' ADBABC '

S.swapcase ()

3. Little exercise Three: ABCDEFGCCC the string into the order of the first C to F and then output the entire string

s1= ' ABCDEFGCCC '

S2= "

Flag=true

For I in S1:

If i== ' C ' and flag:

s2+= ' F '

Flag=false

Else

S2+=i

Print S2

The result of the operation is:

4. Small exercise four: output of 3 of the number such as :34,153 , etc.

print [x for x in range (1001) if ' 3 ' in str (x)]

The above practice may not all of you beginners can understand, do not worry, follow-up python Learning notes we will learn every detail one by one .

Note five: Python string

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.