How to concatenate items in a list to a single string?

后端 未结 11 1931
北荒
北荒 2020-11-21 05:59

Is there a simpler way to concatenate string items in a list into a single string? Can I use the str.join() function?

E.g. this is the input [\'t

相关标签:
11条回答
  • 2020-11-21 06:04

    Although @Burhan Khalid's answer is good, I think it's more understandable like this:

    from str import join
    
    sentence = ['this','is','a','sentence']
    
    join(sentence, "-") 
    

    The second argument to join() is optional and defaults to " ".

    EDIT: This function was removed in Python 3

    0 讨论(0)
  • 2020-11-21 06:05

    It's very useful for beginners to know why join is a string method.

    It's very strange at the beginning, but very useful after this.

    The result of join is always a string, but the object to be joined can be of many types (generators, list, tuples, etc).

    .join is faster because it allocates memory only once. Better than classical concatenation (see, extended explanation).

    Once you learn it, it's very comfortable and you can do tricks like this to add parentheses.

    >>> ",".join("12345").join(("(",")"))
    Out:
    '(1,2,3,4,5)'
    
    >>> list = ["(",")"]
    >>> ",".join("12345").join(list)
    Out:
    '(1,2,3,4,5)'
    
    0 讨论(0)
  • 2020-11-21 06:07

    Without .join() method you can use this method:

    my_list=["this","is","a","sentence"]
    
    concenated_string=""
    for string in range(len(my_list)):
        if string == len(my_list)-1:
            concenated_string+=my_list[string]
        else:
            concenated_string+=f'{my_list[string]}-'
    print([concenated_string])
        >>> ['this-is-a-sentence']
    

    So, range based for loop in this example , when the python reach the last word of your list, it should'nt add "-" to your concenated_string. If its not last word of your string always append "-" string to your concenated_string variable.

    0 讨论(0)
  • 2020-11-21 06:09
    list = ['aaa', 'bbb', 'ccc']
    
    string = ''.join(list)
    print(string)
    >>> aaabbbccc
    
    string = ','.join(list)
    print(string)
    >>> aaa,bbb,ccc
    
    string = '-'.join(list)
    print(string)
    >>> aaa-bbb-ccc
    
    string = '\n'.join(list)
    print(string)
    >>> aaa
    >>> bbb
    >>> ccc
    
    0 讨论(0)
  • 2020-11-21 06:11

    This will help for sure -

    arr=['a','b','h','i']     # let this be the list
    s=""                      # creating a empty string
    for i in arr:
       s+=i                   # to form string without using any function
    print(s) 
    
    
           
    
    0 讨论(0)
  • 2020-11-21 06:18

    If you want to generate a string of strings separated by commas in final result, you can use something like this:

    sentence = ['this','is','a','sentence']
    sentences_strings = "'" + "','".join(sentence) + "'"
    print (sentences_strings) # you will get "'this','is','a','sentence'"
    

    I hope this can help someone.

    0 讨论(0)
提交回复
热议问题