Python Common methods

Source: Internet
Author: User

Python Strip () method

Describe:

The Python strip () method removes the character specified by the String's kinsoku (the default is a space).

Grammar:

Str.strip ([chars])

Parameters:

Chars--removes the character specified by the tail of the String.

Instance:

# !/usr/bin/python  "0000000this is string example....wow!!! 0000000"; Print ' 0 ' );

Operation Result:

 is String example....wow!!!

Python Split () method

Describe:

Python split () slices The string by specifying a delimiter, and separates only the NUM substring if the parameter num has a specified value.

Grammar:

Str.split (str="", Num=string.count (str))

Parameters:

str--delimiter, The default is a space.

Num--the Number of Splits.

Return value:

Returns the segmented list of strings.

Instance:

# !/usr/bin/python  "line1-abcdef \nline2-abc \nline4-abcd"; Print Str.split (); print str.split (', 1);

Operation Result:

['line1-abcdef'line2-abc'line4-abcd ' ['line1-abcdef'\nline2-abc \nline4-abcd' ]

Python various ways to remove whitespace:

"XYZ". Strip ()#returns "xyz""XYZ". Lstrip ()#returns "xyz""XYZ". Rstrip ()#returns "xyz""x y z". replace (' ',"')#returns "xyz"

The conversions between lists, tuples, and strings are implemented by the four functions of join (), str (), list (), tuple ().

    • Use list to convert a string and a tuple into a list
>>> demo_str = ' Test ' >>> demo_tuple = (' t ', ' e ', ' s ', ' t ') >>>demo_list = [' t ', ' e ', ' s ', ' t ']> >> temp = List (demo_tuple) >>> type (temp) <type ' list ' >>>> temp = list (demo_str) >> > Type (temp) <type ' list ' >

    • Use tuple () to convert strings and lists into tuples
>>> demo_str = ' Test ' >>> demo_tuple = (' t ', ' e ', ' s ', ' t ') >>>demo_list = [' t ', ' e ', ' s ', ' t ']> >> temp = tuple (demo_str) >>> type (temp) <type ' tuple ' >>>> temp = tuple (demo_list) >> > Type (temp) <type ' tuple ' >
    • Strings and lists can be converted to strings using str ()
>>> demo_str = ' Test ' >>> demo_tuple = (' t ', ' e ', ' s ', ' t ') >>>demo_list = [' t ', ' e ', ' s ', ' t ']> >> temp = str (demo_list) >>> type (temp) <type ' str ' >>>> temp = str (demo_tuple) >>> Type (temp) <type ' str ' >
Note

Strings converted with Str () cannot be displayed as strings with the print () function

>>> demo_str = ' Test ' >>> demo_tuple = (' t ', ' e ', ' s ', ' t ') >>>demo_list = [' t ', ' e ', ' s ', ' t ']> >> temp = str (demo_list) >>> type (temp) <type ' str ' >>>>print (temp) [' t ', ' e ', ' s ', ' t ']> >> temp = str (demo_tuple) >>> type (temp) <type ' str ' >>>>print (temp) (' t ', ' e ', ' s ', ' t ')

For this problem, use the Join () function to handle

>>> demo_str = ' Test ' >>> demo_tuple = (' t ', ' e ', ' s ', ' t ') >>>demo_list = [' t ', ' e ', ' s ', ' t ']> >> temp = '. join (demo_list) >>> type (temp) <type ' str ' >>>>print (temp) test>>> temp = '. Join (demo_tuple) >>> type (temp) <type ' str ' >>>>print (temp) test

The string type is generated with join () and str (), But why is the result different from print output?

Python Common methods

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.