Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python "join" on both sides of strings?

l = ['1','2','3']
goal = ['<li>1</li>','<li>2</li>']

How can I get goal from l?

I'm playing with list comprehensions but it's messy!

like image 421
matt Avatar asked Jun 07 '12 23:06

matt


People also ask

How do I join two strings together?

Concatenation is the process of appending one string to the end of another string. 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.

What is join () in Python?

join() is an inbuilt string function in Python used to join elements of the sequence separated by a string separator. This function joins elements of a sequence and makes it a string. Syntax: string_name.join(iterable)

How do you join a list of strings in Python?

Use str. join() to join a list of strings. Call str. join(iterable) to join each element in the list of strings iterable with each element connected by the separator str .


1 Answers

Try string formatting and list comprehension, like so.

goal = ['<li>{0}</li>'.format(x) for x in l]
like image 79
cheeken Avatar answered Oct 05 '22 19:10

cheeken