Case:
Here's a list of how to stitch this list up into a string?
L = [1, 2, 3, 4, ' A ', ' B ', ' C ']
What are the methods?
Method 1: for iterative stitching
#!/usr/bin/python3def Go_str (L): median = ' for i in L: # To determine if the string type is if isinstance (i, str): median + = i # if non-string, convert to string else: i = str (i) median + = i return medianif __name__ = = ' __main__ ': # Initialization L = List L = [1, 2, 3, 4, 4.5, ' a ', ' B ', ' C '] new_str = go_str (l) print (NEW_STR)
Method 2: Splicing by join
#!/usr/bin/python3def Go_str (L): # list parsing, [str (i) for I in L], but not optimal, will generate a list again # return '. Join ([STR (i) for i in L]) # generates an iterator (str (i) for I in L), and no additional parentheses return ' is required in the join . Join (STR (i) for I in L) if __name__ = = ' __main__ ':
# Initialize l list L = [1, 2, 3, 4, 4.5, ' a ', ' B ', ' C '] new_str = go_str (l) print (NEW_STR)
Python_ How to stitch multiple small characters into large characters?