Python_ How to stitch multiple small characters into large characters?

Source: Internet
Author: User

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?

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.