How do I move the second word BEFORE the first? [duplicate] - python

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]))

Related

How to remove duplicate string in string in python [duplicate]

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)

Remove a specified character while i using print in python [duplicate]

This question already has answers here:
Print a list of space-separated elements
(4 answers)
Closed 2 years ago.
Hi: I am new to python and programing.
I have a silly question that I couldn't solve.
a=[1,2,3,4,5]
for i in a:
print(i,end=' ')
will get a out put:
1 2 3 4 5
There are space between each numbers in the system out print: ( s means space)
1s2s3s4s5s
How can I remove the last space? The correct out put will be :
1s2s3s4s5
a=[1,2,3,4,5]
print(' '.join([str(x) for x in a])
This will first convert each element of 'a' to string and then join all element using join(). It must be noted that ' ' can be replaced with any other symbol as well
There are various ways, but a simple way is join:
print(' '.join(a), end='')
You can also directly use print:
print(*a, sep=' ', end='')

Is there a way to separate a string into separate words in python? [duplicate]

This question already has answers here:
How do I split a string into a list of words?
(9 answers)
Closed 3 years ago.
I'm trying to separate a long string into different words that I will print off with a .1 second delay on the same line.
You can split according to the space between them:
from time import sleep
string = "hello world"
words = string.split(' ') # splits the string into parts divided by ' '
for w in words:
print(w, end=' ')
sleep(0.1) #delays for 0.1 seconds

How to change a split str into a normal str [duplicate]

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'

Python - how to remove commas, square brackets and quotation marks from list [duplicate]

This question already has answers here:
Pythonic way to print list items
(13 answers)
Closed 7 years ago.
How do I remove the quote marks and commas and brackets from this result:
encrypt = input("enter your string: ")
encrypt = encrypt.replace(" ","")
encrypt_list = [encrypt[i:i+5] for i in range(0, len(encrypt), 5)]
print (encrypt)
print (encrypt_list)
If the input was: 5 blocks of text test
The output is: ['5bloc', 'ksoft', 'extte', 'st']
I need it to be: 5bloc ksoft extte st
You can use str.join, like this:
>>> s = ' '.join(['5bloc', 'ksoft', 'extte', 'st'])
>>> print(s)
5bloc ksoft extte st

Categories