Categories > Coding > Python >

Reverse a String in python


New Reply

UlaDeia

Senior Developer

Posts: 14

Threads: 9

Joined: Jul, 2022

Reputation: 4

  • 0

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

 

 

Posts: 538

Threads: 84

Joined: Mar, 2022

Reputation: 30

  • 1

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

UlaDeia

Senior Developer

Mention

Posts: 14

Threads: 9

Joined: Jul, 2022

Reputation: 4

  • 0

Replied

@Alternate,

Thank you so much man really appreciate your help and time.

eb_

Formally known as Shade.

vip Mention

Posts: 1053

Threads: 3

Joined: Jun, 2020

Reputation: 41

  • 1

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

Murz

PixelPenguin

Mention

Posts: 217

Threads: 16

Joined: Jul, 2021

Reputation: 25

  • 1

Replied

@eb_,

 

Ula is my favourite person ;d

 

Ty for rep: Swiney, Byoke, Lion, Locust, Waves, Weeb, Nickk, darkn, Atari, CubeFaces, Lux14, Rice, Delta, Syraxes, Aeon, and Jordan

UlaDeia

Senior Developer

Mention

Posts: 14

Threads: 9

Joined: Jul, 2022

Reputation: 4

  • 0

Replied

@eb_, hehe okay mr robot.

  • 0

Added

@Murz

 

Thank you and now you are my fav person

Murz

PixelPenguin

Mention

Posts: 217

Threads: 16

Joined: Jul, 2021

Reputation: 25

  • 0

Replied

@UlaDeia,

 

yay, ;D

 

Ty for rep: Swiney, Byoke, Lion, Locust, Waves, Weeb, Nickk, darkn, Atari, CubeFaces, Lux14, Rice, Delta, Syraxes, Aeon, and Jordan

Posts: 113

Threads: 15

Joined: Aug, 2022

Reputation: 6

  • 0

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

Posts: 5

Threads: 1

Joined: Jan, 2023

Reputation: 2

  • 0

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

 

 


New Reply

Users viewing this thread:


( Members: 0, Guests: 1, Total: 1 )