# Letter-based Strings not Recognized as Strings in Python Reversing

Gold Member
Summary:
Algorithm correctly reverses numerical strings but does not recognize letter strings
Hi,
I have a simple Python algorithm in my Jupyter notebook , to reverse a string.
It works well on numerical strings, but when I try to reverse a letter string, I get an error message,
e.g., the string abc, I get the error message:
name 'abc' is not defined

This is my Python code

Python:
def reverse(x):
String=str(x)
if String[0]=='-':
return( '-' + str[:0:-1])
else:
return( str[::-1])
It works no problem with numerical input, e.g., if I enter:
reverse(-235), it returns -532

But if I try reverse(abc), I get the error message above:
"Name 'abc' not defined"

I have no idea what the issue could be. Any ideas?

jedishrfu
Mentor
Look at your return statements I think you conflated the str() function with the variable String.

Personally I would use something more descriptive and less looking like a keyword ie I would use something like s instead of String.

Python:
s="hello"
s[::-1]

WWGD
Gold Member
Look at your return statements I think you conflated the str() function with the variable String.

Personally I would use something more descriptive and less looking like a keyword ie I would use something like s instead of String.

Python:
s="hello"
s[::-1]
Good point, but in my original code I did not make that mistake.
It was:
[code = python]
def reverse(x):
string=str(x)
if string[0]=='-':
return('-' + string[:0:-1])
else:
return(string[::-1])
[/code]

jedishrfu
Mentor
okay your last return is missing a right paren at the end of the statement. Maybe that was the cause of your error message.

WWGD
Gold Member
Good point, but in my original code I did not make that mistake.
It was:
[code =python]
def reverse(x):
string=str(x)
if string[0]=='-':
return('-' + string[:0:-1])
else:
return(string[::-1]
[/code]
Summary:: Algorithm correctly reverses numerical strings but does not recognize letter strings

Hi,
I have a simple Python algorithm in my Jupyter notebook , to reverse a string.
It works well on numerical strings, but when I try to reverse a letter string, I get an error message,
e.g., the string abc, I get the error message:
name 'abc' is not defined

This is my Python code

Python:
def reverse(x):
String=str(x)
if String[0]=='-':
return( '-' + str[:0:-1])
else:
return( str[::-1])
It works no problem with numerical input, e.g., if I enter:
reverse(-235), it returns -532

But if I try reverse(abc), I get the error message above:
"Name 'abc' not defined"

I have no idea what the issue could be. Any ideas?

okay your last return is missing a right paren at the end of the statement. Maybe that was the cause of your error message.
Really sorry for my carelessness, Jedi, but I did not make that mistake in the original either. I will be extra careful to type in code correctly. The one you suggested does work without a problem.

Mark44
Mentor
okay your last return is missing a right paren at the end of the statement. Maybe that was the cause of your error message.
Almost certainly that was the case.

This works:
Python:
def reverse(x):
string=str(x)
if string[0]=='-':
return('-' + string[:0:-1])
else:
return(string[::-1])

str1 = 'abc'
str2 = reverse(str1)
print(str2)

WWGD
Gold Member
Ok, my problem was fixed just by using the original code but entering strings within double quotes, i.e.
reverse("stringname")
Go figure.
Edit: @Mark44 suggestion also worked out. Thank you both.

Ibix
2020 Award
Ok, my problem was fixed just by using the original code but entering strings within double quotes, i.e.
reverse("stringname")
Yes. The error is python telling you that it's never heard if a variable called abc. Adding quotes turns the input into the literal string you wanted. The program works with numeric input because numbers aren't legal variable names, so are always interpreted as literally what you typed. Your str(x) line turns it into the character string "532", which is then reversed.

WWGD
PeterDonis
Mentor
2020 Award
I have a simple Python algorithm in my Jupyter notebook , to reverse a string.
Why would you want to keep a "-" at the beginning for a string that doesn't contain all numeric characters?

WWGD
Gold Member
Why would you want to keep a "-" at the beginning for a string that doesn't contain all numeric characters?
True, but it seems it is used just in case it is numeric and ignored if it is not.

PeterDonis
Mentor
2020 Award
True, but it seems it is used just in case it is numeric and ignored if it is not.
No, it isn't. There is no test anywhere in the code for an all-numeric string. It just tests for the first character being "-". So we would get, for example, reverse("-xyz") == "-zyx".

WWGD
PeterDonis
Mentor
2020 Award
This works
One note: there is no need for parentheses in the return statements. They work (since putting parentheses around any Python expression gives a valid Python expression), but they're not necessary.

WWGD
Gold Member
No, it isn't. There is no test anywhere in the code for an all-numeric string. It just tests for the first character being "-". So we would get, for example, reverse("-abc") == "-cba".
To address the example you brought up; that of -abc, -3ab, etc. , which may or may not be numeric.

pbuk
Gold Member
Are you using a, b, c... as hexadecimal digits? If so:
• This is not the way to do it, you want -0x3ab
• Reversing digits of a decimal number and a hexadecimal number are different operations so mixing them in the same program is undesirable
What are you actually trying to do?

Gold Member
Are you using a, b, c... as hexadecimal digits? If so:
• This is not the way to do it, you want -0x3ab
• Reversing digits of a decimal number and a hexadecimal number are different operations so mixing them in the same program is undesirable
What are you actually trying to do?
These are interview questions. This question was about an algorithm to reverse strings; no more context was given.

pbuk
PeterDonis
Mentor
2020 Award
To address the example you brought up; that of -abc, -3ab, etc. , which may or may not be numeric.
Yes, but "-xyz" is clearly not, yet it behaves the same way; see post #11.

Ibix