converting a string to a readable sentence [duplicate] - python

This question already has answers here:
How do I convert a list into a string with spaces in Python?
(6 answers)
Closed 4 years ago.
say you have a list like so:
lst = ['my', 'name', 'is', 'jack.']
If I convert to a string doing this:
''.join(lst)
output
'mynameisjack.'
How do I make the list print out:
"my name is jack."
instead of all together.

Instead of using ''.join(lst)(an empty string), use ' '.join(lst), with a space (see the documentation of join!).

Related

how to convert space to underscore in python list [duplicate]

This question already has answers here:
Find and replace string values in list [duplicate]
(6 answers)
How to change the list separator in python?
(2 answers)
Replacing instances of a character in a string
(17 answers)
Closed 2 years ago.
I have a list like this ['BASE', 'BASE xBU xPY', 'BU GROUP REL', 'PY REL'] and I want to convert it to ['BASE', 'BASE_xBU_xPY', 'BU_GROU_ REL', 'PY_REL'] in short convert space to underscore for value in b/w quotes
I would suggest using replace along with map
Example:
my_list = ['BASE', 'BASE xBU xPY', 'BU GROUP REL', 'PY REL']
converter = lambda x: x.replace(' ', '_')
my_list = list(map(converter, my_list))
my_list
['BASE', 'BASE_xBU_xPY', 'BU_GROUP_REL', 'PY_REL']

How to make a word of all letters in a list? (Python) [duplicate]

This question already has answers here:
How to concatenate (join) items in a list to a single string
(11 answers)
Closed 3 years ago.
How can I make a word from all the letters in a string?
For example:
(this is my list)
["h","e","l","l","o"]
And I want this as output:
hello
Try this :
"".join(["h","e","l","l","o"])
Use the join function which concatenate all the characters/substrings present in the list & return a single string.
name = ["h","e","l","l","o"]
concat_name = "".join(name)
print(concat_name)
Output :
hello
Try this :
''.join(["h","e","l","l","o"])

How to split a input to each character? [duplicate]

This question already has answers here:
How do I split a string into a list of characters?
(15 answers)
Closed 5 years ago.
Suppose I get input as apple, how can I split it in list of each character like ['a','p','p','l','e']?
I tried [i for i in input().split('')],
but it outputs error: ValueError: empty separator.
Try list('apple').
This will split the string 'apple' into individual characters and place them inside a list.
Output:
['a','p','p','l','e']

Convert a list of characters in a single List , using a List comprehension [duplicate]

This question already has answers here:
Convert a list of characters into a string [duplicate]
(9 answers)
Closed 6 years ago.
I have this list :
listOfWords = ["this","is","a","list","of","words"]
and I need to have [this is a list of words], with a List comprehension.
You mean a list with a single string item?
>>> [' '.join(["this","is","a","list","of","words"])]
['this is a list of words']

How to convert a string into a list in python [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Converting a string into a list in Python
How to convert a string into a list in python such as : 'LIFE IS FULL' to ['LIFE','IS','FULL']
use str.split():
>>> strrs="LIFE IS FULL"
>>> strrs.split()
['LIFE', 'IS', 'FULL']
You could use the .split method:
a = "LIFE IS FULL"
b = a.split()
['LIFE','IS','FULL']

Categories