Categories > Coding > Python >
Reverse a String in python
Posted
I'm writing a Python software that writes out the entire song '99 bottles of beer' in reverse. The only thing I can't reverse are the numbers, which are integers rather than strings.
This is my entire script,
def reverse(str):
return str[::-1]
def plural(word, b):
if b != 1:
return word + 's'
else:
return word
def line(b, ending):
print b or reverse('No more'), plural(reverse('bottle'), b), reverse(ending)
for i in range(99, 0, -1):
line(i, "of beer on the wall")
line(i, "of beer"
print reverse("Take one down, pass it around")
line(i-1, "of beer on the wall \n")
I understand that my reverse function accepts a text as an argument after reading this article, but I don't know how to take in an integer or how to reverse the number later in the script.
Thank you in Advance for your help
Replied
First of all, you have multiple syntax errors that prevent the code from even running in the first place. Additionally, in the plural function you'd want to put the 's' before the word variable otherwise it will return as 'elttobs' (reversed = 'sbottle') rather than 'selttob' (reversed = 'bottles').
Assuming you're trying to reverse the integers where 96 would be 69, all you have to do is in the 'line' function change the 'b' to 'reverse(str(b))'. This converts the integer into a string and then reverses it. The fixed code is below:
def reverse(str):
return str[::-1]
def plural(word, b):
if b != 1:
return 's' + word
else:
return word
def line(b, ending):
print(reverse(str(b)) or reverse('No more'), plural(reverse('bottle'), b), reverse(ending))
for i in range(99, 0, -1):
line(i, "of beer on the wall")
line(i, "of beer")
print(reverse("Take one down, pass it around"))
line(i-1, "of beer on the wall \n")
I also fixed the syntax errors for you.
test_bot2 on top
#FREETEST_BOT2
jon is scared of me https://imgur.com/Fd7Ryyt.png
Replied
Thank you so much man really appreciate your help and time.
Replied
im convicned this guy is an advanced bot running on chat gpt gathering more nautral data.
https://media.discordapp.net/attachments/1010636204225601659/1012865624797610044/sKQybOLT.gif
https://media.discordapp.net/attachments/1074747951714414596/1078053209211424799/image.png
Replied
Ty for rep: Swiney, Byoke, Lion, Locust, Waves, Weeb, Nickk, darkn, Atari, CubeFaces, Lux14, Rice, Delta, Syraxes, Aeon, and Jordan
Replied
@eb_, hehe okay mr robot.
Added
Replied
Ty for rep: Swiney, Byoke, Lion, Locust, Waves, Weeb, Nickk, darkn, Atari, CubeFaces, Lux14, Rice, Delta, Syraxes, Aeon, and Jordan
Replied
Reversing a string is not hard.
string = "Hello, world!"
reversed_string = string[::-1]
print(reversed_string)
This returns "!dlrow ,olleH"
hecker dude ngl i hecked 5 ips in 1 second also luaU_loadbiglongjuicythingy(rL);
Replied
Hello this is Gulshan Negi
Well, you can start with simple code for example:-
string = "Hello World"
reversed_string = string[::-1]
print(reversed_string)
In this code, we first define a string variable called "string" which contains the string we want to reverse. We then use slicing to reverse the string by specifying the step value as -1, which means that we start from the end of the string and move towards the beginning, taking every character along the way.
Output:-
dlroW olleH
Hope it will clear your all doubts and help.
Thanks
Users viewing this thread:
( Members: 0, Guests: 1, Total: 1 )