Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python 3 string.join() equivalent?

I've been using string.join() method in python 2 but it seems like it has been removed in python 3. What is the equivalent method in python 3?

string.join() method let me combine multiple strings together with a string in between every other string. For example, string.join(("a", "b", "c"), ".") would result "a.b.c".

like image 522
Dennis Avatar asked Dec 29 '11 12:12

Dennis


People also ask

How do you join strings in Python 3?

Joining Strings with the '+' Operator. Concatenation is the act of joining two or more strings to create a single, new string. In Python, strings can be concatenated using the '+' operator. Similar to a math equation, this way of joining strings is straight-forword, allowing many strings to be “added” together.

What is join () in Python?

The Python join() function is used to join all the elements from the iterable and create a string and return it as an output to the user. Python join() returns a new string which is the concatenation of the other strings in the iterable specified.

How do you join three strings together?

You concatenate strings by using the + operator. For string literals and string constants, concatenation occurs at compile time; no run-time concatenation occurs. For string variables, concatenation occurs only at run time.

How do you join words in Python?

join() function in Python Combine them into a sentence with the join(sequence) method. The method is called on a seperator string, which can be anything from a space to a dash. This is easier than using the plus operator for every word, which would quickly become tedious with a long list of words.


2 Answers

'.'.join() or ".".join().. So any string instance has the method join()

like image 142
Tim Avatar answered Oct 04 '22 21:10

Tim


str.join() works fine in Python 3, you just need to get the order of the arguments correct

>>> str.join('.', ('a', 'b', 'c')) 'a.b.c' 
like image 40
hobs Avatar answered Oct 04 '22 20:10

hobs