Python string join method

title character string join method of operation

Usually, we get a string, the string need treatment process, which means that cleaning data, we can use common string operations are widely used in web crawler and web application development, server and transportation peacekeeping and other aspects. For example, reptiles need to use the re is a regular expression string processing, improve the efficiency of our reptiles, in terms of operation and maintenance, we need to be concerned about the data that we need through the extraction system log string, then attempt to , tables, etc. in the form of the command to complete the operation and maintenance tasks, therefore, a string of IT people is a form of data often requires contact.
Say today, is the conventional method of the string, join, it translates stitching.
Mosaic format string is' string'.join (iterable), string represents the things that need stitching, iterable object can represent iterations, then this iterables need to pay attention, it can be a tuple may be a list, a single character string, but after stitching, if you use a variable called, then the type of the variable is a string.
Below using the code to be described
demo_tuple = ( 'a', ' b', 'c') # This is a tuple
print (type (demo_tuple), demo_tuple ) # print test whether the tuple, and the entire print
demo_tuple1 = ' '.join (demo_tuple) # ** characters inserted between each element of the tuple, but does not include the head and tail
print (type (demo_tuple1), demo_tuple1 ) # newly defined test variable types and printing
is now above the code we We found, tuples join method may be used to insert elements, and generates a new string. Similarly, strings, lists, are also possible.
So, if an element in the iterable is not the same type of it?
The answer is no, at this time will certainly be given, must be a non-integer and floating-point unified element types, see the code:
= demo_list [ 'A', 'B', 'C', l, 2,3]
Print (type (demo_list), demo_list)
demo_list1 = '
' .join (demo_list)
Print (type (demo_list1), demo_list1)
TypeError: sequence item 3: expected str instance, int found
translation is wrong type, within the compiler does not support element 3 is str instance, found int.
demo_list = '. 1', '2', '. 3'
Print (type (demo_list), demo_list)
demo_list1 = '*' the Join (demo_list [0:. 6]). # has been out of range without being given
print (type (demo_list1), demo_list1)
thus, the conclusion, join () method, only the string inside stitching, head and tail void, generates a new string splicing, join object must be free floating type integer iterables, and join () method does not cross-border problems.

Published 13 original articles · won praise 0 · Views 307

Guess you like

Origin blog.csdn.net/alwaysbefine/article/details/104707739