Get the App
SLTechnology News&Howtos  ›  Development  › 

How does python connect strings with .join ()

Shulou Source: shulou.com Published: 2022-06-01 05:15:34 11月14日 Update

This article mainly introduces how python uses .join () to connect strings, with a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, let the editor take you to understand it.

Concatenate strings with .join ()

Suppose we have a list of different strings, and we want to combine all the elements into one string, with each word separated by a space. The bad way is to do this:

List_of_strings = ["Hello", "my", "friend"] # BAD: my_string = "" for i in list_of_strings: my_string + = I + ""

We define an empty string, then iterate through the list, and then append words and spaces to the string. You should know that strings are immutable elements, so here we have to create new strings every time. For large lists, this code can be very slow, so you should forget this method immediately! Better, faster, and more concise are the .join () methods:

.join () method: # GOOD: list_of_strings = ["Hello", "my", "friend"] my_string = ".join (list_of_strings) Thank you for reading this article carefully. I hope the article" how to use python to connect strings with join () "shared by the editor will be helpful to you. At the same time, I hope you will support us, pay attention to the industry information channel, and more related knowledge is waiting for you to learn!

Tags: Characters strings articles methods elements words spaces differences brevity between code value interest at the same time more faster friends knowledge empty characters ribbon Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS vpn MySQL NVidia Docker