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