Categories > Coding > Python >

Reverse a String in python

UlaDeia

Senior Developer

Posts: 14

Threads: 9

Joined: Jul, 2022

Reputation: 2

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

 

 

  • 0

Alternate

stop take my rice

vip

Posts: 711

Threads: 115

Joined: Mar, 2022

Reputation: 36

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.

  • 1

we are dead

UlaDeia

Senior Developer

Posts: 14

Threads: 9

Joined: Jul, 2022

Reputation: 2

Replied

@Alternate,

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

  • 0

eb_

Formally known as Shade

vip

Posts: 1044

Threads: 4

Joined: Jun, 2020

Reputation: 44

Replied

im convicned this guy is an advanced bot running on chat gpt gathering more nautral data.

  • 1

https://media.discordapp.net/attachments/1010636204225601659/1012865624797610044/sKQybOLT.gif

Murz

PixelPenguin

Posts: 239

Threads: 20

Joined: Jul, 2021

Reputation: 28

Replied

@eb_,

 

Ula is my favourite person ;d

  • 1

 

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

P.S, I like cats better too!

UlaDeia

Senior Developer

Posts: 14

Threads: 9

Joined: Jul, 2022

Reputation: 2

Replied

@eb_, hehe okay mr robot.

  • 0

Added

@Murz

 

Thank you and now you are my fav person

  • 0

Murz

PixelPenguin

Posts: 239

Threads: 20

Joined: Jul, 2021

Reputation: 28

Replied

@UlaDeia,

 

yay, ;D

  • 0

 

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

P.S, I like cats better too!

heckerdude

heckerdude

Posts: 128

Threads: 16

Joined: Aug, 2022

Reputation: 6

Replied

Reversing a string is not hard.

string = "Hello, world!"
reversed_string = string[::-1]
print(reversed_string)

This returns "!dlrow ,olleH"
  • 0

hecker dude ngl i hecked 5 ips in 1 second also luaU_loadbiglongjuicythingy(rL);

Posts: 1

Threads: 0

Joined: Mar, 2023

Reputation: 0

Replied

It is significant to remember that you might need to work with an attorney to guide you through the procedure. You can learn the law and bargain with your husband for a fair settlement with the aid of an attorney.  An uncontested divorce in Virginia occurs when all of the issues in the divorce are agreed upon by both parties. This includes alimony, the division of assets and debts, child custody, and child support. Searching local attorneys who focus on family and divorce law is the best approach to discover one for Virginia uncontested divorce. Our knowledgeable attorneys are on hand to assist you.

Website: https://srislawyer.com/uncontested-divorce-lawyer-virginia/

  • 0

There are additional procedures that must be followed if you are seeking for an uncontested divorce in Virginia with a child. Reach our team.

 

Users viewing this thread:

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