This question already has an answer here:
How can I join a list of string into one string in Python
(1 answer)
Closed 5 years ago.
I have a code that does
a = "hello world"
a.split(' ')
then it does other things and after that when it prints it prints like this
['hello','world']
how would i make it change back to printing
hello word
Thanks for the help in advance!
Given a list, you can use ' '.join:
s = ['hello','world']
new_s = ' '.join(s)
Output:
'hello world'
Related
This question already has answers here:
How can I tell if a string repeats itself in Python?
(13 answers)
Closed 1 year ago.
I want to keep a single sentence in string with many the same sentences.
Input:
str1 = 'I want to do this to help her to get the job done I want to do this to help her to get the job done'
Expected output:
str2 = 'I want to do this to help her to get the job done'
Not missing some "to". Thanks.
out = []
for word in str1.split():
if word not in out:
out.append(word)
res = " ".join(out)
This question already has answers here:
Reverse each word in a string
(7 answers)
Closed 3 years ago.
How to reverse each word in a sentence without affecting special characters in Python?
Suppose input:
" Hi, I am vip's "
Output should be like:
" iH, I ma piv's"
One approach, using re.sub with a callback function:
inp = " Hi, I am vip's "
output = re.sub(r'\w+', lambda x: x.group(0)[len(x.group(0))::-1], inp)
print(inp)
print(output)
This prints:
Hi, I am vip's
iH, I ma piv's
Try this:
>> strung = 'this is my string'
>> ' '.join(x[::-1] for x in strung.split())
'siht si ym gnirts'
This question already has answers here:
How to convert comma-delimited string to list in Python?
(7 answers)
Closed 3 years ago.
I am new to Python and i have a problem of converting the strings of words into a list of word and my string will be downward like this.
Hello 1
Hello 2
Hello 3
I want to convert to a list example: List=['Hello 1','Hello 2','Hello 3']
List[0]='Hello 1'
...
How to do this? any help will appreciate.
use split function:
hello_str = "hello1 hello2 hello3"
hello_list = hello_str.split()
this will result
['hello1', 'hello2', 'hello3']
Try this code:
st="Hello 1 \n Hello 2 \n Hello 3"
ArrStr=st.split("\n")
print(ArrStr)
This question already has answers here:
Reverse the ordering of words in a string
(48 answers)
Closed 4 years ago.
If my variable is:
string = "first second"
How can I change that to print:
second first
annoyingly simple but I can't find a solution for it!
I'd split the string on ' ' and then join it back:
s = 'first second'
tmp = s.split(' ')
result = ' '.join((tmp[1], tmp[0]))
This question already has answers here:
Is there a simple way to remove multiple spaces in a string?
(27 answers)
Closed 6 years ago.
I wanna know how to remove unwanted space in between a string. For example:
>>> a = "Hello world"
and i want to print it removing the extra middle spaces.
Hello world
This will work:
" ".join(a.split())
Without any arguments, a.split() will automatically split on whitespace and discard duplicates, the " ".join() joins the resulting list into one string.
Regular expressions also work
>>> import re
>>> re.sub(r'\s+', ' ', 'Hello World')
'Hello World'