This question already has answers here:
How do I reverse a string in Python?
(19 answers)
Closed last year.
for example I entered follwoing string ;
" hello I am Mohsen" ;
now I want to Print on output :
"Mohsen am I hello "
please help me !
Both Corralien's and Tobi208's works, and can be combined to the shorter version;
s = "hello I am Mohsen"
print(' '.join(s.split(' ')[::-1]))
or if you want to input the string in the terminal as a prompt;
s = input()
print(' '.join(s.split(' ')[::-1]))
Split by space, reverse the list, and stitch it back together.
s = " hello I am Mohsen"
words = s.split(' ')
words_reversed = words[::-1]
s_reversed = ' '.join(words_reversed)
print(s_reversed)
Related
This question already has answers here:
Split a string by spaces -- preserving quoted substrings -- in Python
(16 answers)
Closed 12 months ago.
Is it possible to split() string at " "? I have tried
x=str(input("Enter string to split: ")) #Input: one "two three"
xsplit=str.split()
print(xsplit[1])
but it returns "two" but I want it to show "two three".
How to do it?
This will let you split on the space as well as gather everything after the split.
x=str(input("Enter string to split: ")) #Input: one "two three"
xsplit = x.split(' ')[1:]
xjoin = ' '.join(xsplit)
print(xjoin)
Once you have split into a list you can rejoin the list into a single string with .join(xsplit)
This question already has answers here:
How do I reverse a string in Python?
(19 answers)
Closed 5 years ago.
How would I make a word print backwards in Python? For example, if I have this:
word = input("Enter a word: ")
how would I make that print backwards?
The simplest one, using extended slice :
>>> word[::-1]
#driver values :
IN : word = 'abcd'
OUT : 'dcba'
Try This:
a=input()
print ("".join(reversed(a)))
This question already has answers here:
Join items of a list with '+' sign in a string
(4 answers)
Closed 6 years ago.
For example if I have code:
print("Give me keywords")
keywords = input()
print("Those are nice " + keywords)
lets say I input:
"Banana cocktail"
Now the question is: Is it possible for me to get it print:
"Those are nice Banana + cocktail".
So the idea is to get + sign every time the user puts space in their input.
Either split and join as suggested in the comments, or replace:
print("Give me keywords")
keywords = input()
print("Those are nice " + keywords.replace(' ', ' + '))
Try this:
print("Those are nice ", ' + '.join(keywords.split()))
There are many options:
keywords.replace(' ', ' + ')
' + '.join(keywords.split())
re.sub('\s+', ' + ', keywords)
You should choose the method depending on that how many spaces do you have between words,
want you replace only spaces or some other things etc.
you can split keywords and join the resultant list with '+'
' + '.join(keywords.split(' '))
This question already has answers here:
Why doesn't calling a string method (such as .replace or .strip) modify (mutate) the string?
(3 answers)
Closed 6 years ago.
For some reason string.replace(" ", "") is not working.
Is there another way of removing white spaces from a string?
Why is .replace not working in this particular situation?
string = input('enter a string to see if its a palindrome: ')
string.replace(' ', '') # for some reason this is not working
# not sure why program will only work with no spaces
foo = []
bar = []
print(string)
for c in string:
foo.append(c)
bar.append(c)
bar.reverse()
if foo == bar:
print('the sentence you entered is a palindrome')
else:
print('the sentence you entered is not a palindrome')
replace() returns a new string, it doesn't modify the original. Try this instead:
string = string.replace(" ", "")
This question already has answers here:
How to concatenate (join) items in a list to a single string
(11 answers)
Closed 7 years ago.
def main():
key = []
mess=input('Write Text: ')
for ch in mess:
x = ord(ch)
x = x-3
x = chr(x)
key.append(x)
print("Your code message is: ",key)
outFile = open("Encryptedmessage.txt","w")
print(key, file=outFile)
main()
so far i have written this a it works fine but my problem is the output is
Write Text: the
Your code message is: ['q', 'e', 'b']
and i was wondering how you would get rid of the punction so the output would be
Write Text: the
Your code message is: qeb
key is a list. You can use join(list) to join the elements of the list together:
print("Your code message is: ", "".join(key))
str.join(iterable)
Return a string which is the concatenation of the strings in the
iterable iterable. The separator between elements is the string
providing this method.
Source: https://docs.python.org/2.7/library/stdtypes.html?#str.join
You don't want any separator characters in between the elements of the list, so use an empty string "" as the separator.
Maybe replace
key=[]
with
key=""
and replace
key.append(x)
with
key=key+x
?