Python3 reversing an inputed sentence
I am trying to create a function that reverses a sentence that a user inputs but when I run the program I am not getting the sentence in reverse. Bellow is my code
sentence=input('Enter a sentence: ')
def sentence_reverse(sentence):
words=sentence.split()
newsentence=words.reverse()
return (newsentence)
print(sentence_reverse(sentence))
python-3.x
add a comment |
I am trying to create a function that reverses a sentence that a user inputs but when I run the program I am not getting the sentence in reverse. Bellow is my code
sentence=input('Enter a sentence: ')
def sentence_reverse(sentence):
words=sentence.split()
newsentence=words.reverse()
return (newsentence)
print(sentence_reverse(sentence))
python-3.x
add a comment |
I am trying to create a function that reverses a sentence that a user inputs but when I run the program I am not getting the sentence in reverse. Bellow is my code
sentence=input('Enter a sentence: ')
def sentence_reverse(sentence):
words=sentence.split()
newsentence=words.reverse()
return (newsentence)
print(sentence_reverse(sentence))
python-3.x
I am trying to create a function that reverses a sentence that a user inputs but when I run the program I am not getting the sentence in reverse. Bellow is my code
sentence=input('Enter a sentence: ')
def sentence_reverse(sentence):
words=sentence.split()
newsentence=words.reverse()
return (newsentence)
print(sentence_reverse(sentence))
python-3.x
python-3.x
edited Nov 13 '18 at 8:35
Andreas
1,7761718
1,7761718
asked Nov 13 '18 at 8:12
Caleb DimensteinCaleb Dimenstein
21
21
add a comment |
add a comment |
5 Answers
5
active
oldest
votes
def sentence_reverse(s):
return ' '.join(s.split()[::-1])
print(sentence_reverse(sentence))
add a comment |
you can do this
def reverse(s):
if len(s) == 0:
return s
else:
return reverse(s[1:]) + s[0]
or :
def reverse2(s):
return s[::-1]
They seem to want to reverse by word, not by letter.
– Loocid
Nov 13 '18 at 8:18
add a comment |
reverse()
is an in-place operation, meaning it reverses words
itself and doesnt return anything. So instead of returning newsentence
, you want to return words
like so:
sentence=input('Enter a sentence: ')
def sentence_reverse(sentence):
words=sentence.split()
words.reverse()
return words
print(sentence_reverse(sentence))
>>>Enter a sentence: hello world
>>>['world', 'hello']
add a comment |
Here is a simplest way to solve your problem:
sentence=input('Enter a sentence: ')
def sentence_reverse(sentence):
words= sentence.split() # breaks the sentence into words
rev_sentence= ''
for word in words:
rev_sentence = ' ' + word + rev_sentence
return rev_sentence
print(sentence_reverse(sentence))
Input: Hi please reverse me
Ouput: me reverse please Hi
Hope this helps you. Kindly let me know if anything else is needed.
add a comment |
Welcome to StackOverflow!
The reason is because you are using split()
but it does not convert your input string into the list of its character. It just make a list with one element, which is your input string. Instead, convert the string to list using list()
function, and then convert it back to string using join()
function. In addition to that, reverse()
returns nothing. So, you have to returns the words
variable instead.
sentence=input('Enter a sentence: ')
def sentence_reverse(sentence):
words=list(sentence)
words.reverse()
return ''.join(words)
print(sentence_reverse(sentence))
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53276515%2fpython3-reversing-an-inputed-sentence%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
def sentence_reverse(s):
return ' '.join(s.split()[::-1])
print(sentence_reverse(sentence))
add a comment |
def sentence_reverse(s):
return ' '.join(s.split()[::-1])
print(sentence_reverse(sentence))
add a comment |
def sentence_reverse(s):
return ' '.join(s.split()[::-1])
print(sentence_reverse(sentence))
def sentence_reverse(s):
return ' '.join(s.split()[::-1])
print(sentence_reverse(sentence))
answered Nov 13 '18 at 8:25
Sari MasriSari Masri
1297
1297
add a comment |
add a comment |
you can do this
def reverse(s):
if len(s) == 0:
return s
else:
return reverse(s[1:]) + s[0]
or :
def reverse2(s):
return s[::-1]
They seem to want to reverse by word, not by letter.
– Loocid
Nov 13 '18 at 8:18
add a comment |
you can do this
def reverse(s):
if len(s) == 0:
return s
else:
return reverse(s[1:]) + s[0]
or :
def reverse2(s):
return s[::-1]
They seem to want to reverse by word, not by letter.
– Loocid
Nov 13 '18 at 8:18
add a comment |
you can do this
def reverse(s):
if len(s) == 0:
return s
else:
return reverse(s[1:]) + s[0]
or :
def reverse2(s):
return s[::-1]
you can do this
def reverse(s):
if len(s) == 0:
return s
else:
return reverse(s[1:]) + s[0]
or :
def reverse2(s):
return s[::-1]
edited Nov 13 '18 at 8:18
answered Nov 13 '18 at 8:15
Sari MasriSari Masri
1297
1297
They seem to want to reverse by word, not by letter.
– Loocid
Nov 13 '18 at 8:18
add a comment |
They seem to want to reverse by word, not by letter.
– Loocid
Nov 13 '18 at 8:18
They seem to want to reverse by word, not by letter.
– Loocid
Nov 13 '18 at 8:18
They seem to want to reverse by word, not by letter.
– Loocid
Nov 13 '18 at 8:18
add a comment |
reverse()
is an in-place operation, meaning it reverses words
itself and doesnt return anything. So instead of returning newsentence
, you want to return words
like so:
sentence=input('Enter a sentence: ')
def sentence_reverse(sentence):
words=sentence.split()
words.reverse()
return words
print(sentence_reverse(sentence))
>>>Enter a sentence: hello world
>>>['world', 'hello']
add a comment |
reverse()
is an in-place operation, meaning it reverses words
itself and doesnt return anything. So instead of returning newsentence
, you want to return words
like so:
sentence=input('Enter a sentence: ')
def sentence_reverse(sentence):
words=sentence.split()
words.reverse()
return words
print(sentence_reverse(sentence))
>>>Enter a sentence: hello world
>>>['world', 'hello']
add a comment |
reverse()
is an in-place operation, meaning it reverses words
itself and doesnt return anything. So instead of returning newsentence
, you want to return words
like so:
sentence=input('Enter a sentence: ')
def sentence_reverse(sentence):
words=sentence.split()
words.reverse()
return words
print(sentence_reverse(sentence))
>>>Enter a sentence: hello world
>>>['world', 'hello']
reverse()
is an in-place operation, meaning it reverses words
itself and doesnt return anything. So instead of returning newsentence
, you want to return words
like so:
sentence=input('Enter a sentence: ')
def sentence_reverse(sentence):
words=sentence.split()
words.reverse()
return words
print(sentence_reverse(sentence))
>>>Enter a sentence: hello world
>>>['world', 'hello']
edited Nov 13 '18 at 8:23
answered Nov 13 '18 at 8:16
LoocidLoocid
2,61011230
2,61011230
add a comment |
add a comment |
Here is a simplest way to solve your problem:
sentence=input('Enter a sentence: ')
def sentence_reverse(sentence):
words= sentence.split() # breaks the sentence into words
rev_sentence= ''
for word in words:
rev_sentence = ' ' + word + rev_sentence
return rev_sentence
print(sentence_reverse(sentence))
Input: Hi please reverse me
Ouput: me reverse please Hi
Hope this helps you. Kindly let me know if anything else is needed.
add a comment |
Here is a simplest way to solve your problem:
sentence=input('Enter a sentence: ')
def sentence_reverse(sentence):
words= sentence.split() # breaks the sentence into words
rev_sentence= ''
for word in words:
rev_sentence = ' ' + word + rev_sentence
return rev_sentence
print(sentence_reverse(sentence))
Input: Hi please reverse me
Ouput: me reverse please Hi
Hope this helps you. Kindly let me know if anything else is needed.
add a comment |
Here is a simplest way to solve your problem:
sentence=input('Enter a sentence: ')
def sentence_reverse(sentence):
words= sentence.split() # breaks the sentence into words
rev_sentence= ''
for word in words:
rev_sentence = ' ' + word + rev_sentence
return rev_sentence
print(sentence_reverse(sentence))
Input: Hi please reverse me
Ouput: me reverse please Hi
Hope this helps you. Kindly let me know if anything else is needed.
Here is a simplest way to solve your problem:
sentence=input('Enter a sentence: ')
def sentence_reverse(sentence):
words= sentence.split() # breaks the sentence into words
rev_sentence= ''
for word in words:
rev_sentence = ' ' + word + rev_sentence
return rev_sentence
print(sentence_reverse(sentence))
Input: Hi please reverse me
Ouput: me reverse please Hi
Hope this helps you. Kindly let me know if anything else is needed.
answered Nov 13 '18 at 8:29
Akhilesh PandeyAkhilesh Pandey
539313
539313
add a comment |
add a comment |
Welcome to StackOverflow!
The reason is because you are using split()
but it does not convert your input string into the list of its character. It just make a list with one element, which is your input string. Instead, convert the string to list using list()
function, and then convert it back to string using join()
function. In addition to that, reverse()
returns nothing. So, you have to returns the words
variable instead.
sentence=input('Enter a sentence: ')
def sentence_reverse(sentence):
words=list(sentence)
words.reverse()
return ''.join(words)
print(sentence_reverse(sentence))
add a comment |
Welcome to StackOverflow!
The reason is because you are using split()
but it does not convert your input string into the list of its character. It just make a list with one element, which is your input string. Instead, convert the string to list using list()
function, and then convert it back to string using join()
function. In addition to that, reverse()
returns nothing. So, you have to returns the words
variable instead.
sentence=input('Enter a sentence: ')
def sentence_reverse(sentence):
words=list(sentence)
words.reverse()
return ''.join(words)
print(sentence_reverse(sentence))
add a comment |
Welcome to StackOverflow!
The reason is because you are using split()
but it does not convert your input string into the list of its character. It just make a list with one element, which is your input string. Instead, convert the string to list using list()
function, and then convert it back to string using join()
function. In addition to that, reverse()
returns nothing. So, you have to returns the words
variable instead.
sentence=input('Enter a sentence: ')
def sentence_reverse(sentence):
words=list(sentence)
words.reverse()
return ''.join(words)
print(sentence_reverse(sentence))
Welcome to StackOverflow!
The reason is because you are using split()
but it does not convert your input string into the list of its character. It just make a list with one element, which is your input string. Instead, convert the string to list using list()
function, and then convert it back to string using join()
function. In addition to that, reverse()
returns nothing. So, you have to returns the words
variable instead.
sentence=input('Enter a sentence: ')
def sentence_reverse(sentence):
words=list(sentence)
words.reverse()
return ''.join(words)
print(sentence_reverse(sentence))
edited Nov 13 '18 at 8:37
answered Nov 13 '18 at 8:23
AndreasAndreas
1,7761718
1,7761718
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53276515%2fpython3-reversing-an-inputed-sentence%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown