Python Forum
Need help converting string to int
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need help converting string to int
#5
You can multiply a string by an int. This makes the string repeat. When you multiply '3'x3 you get '3''3''3' or '333'. If you multiply that by 3 again you get '333''333''333' or '333333333'. This happens in your code because of a misplaced parenthesis.
print(int(num * int(num) * int(num)))
# should be
print(int(num) * int(num) * int(num))
As has been mentioned, the time for the conversion is before you call the function. Not only does this make the function more useful, but you only need to perform the conversion once.
Reply


Messages In This Thread
Need help converting string to int - by dedesssse - Jul-07-2021, 03:48 PM
RE: Need help converting string to int - by deanhystad - Jul-07-2021, 05:14 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Converting '1a2b3c' string to Dictionary PythonNoobLvl1 6 1,936 May-13-2022, 03:44 PM
Last Post: deanhystad
  Beautify dictionary without converting to string. sharoon 6 3,473 Apr-11-2021, 08:32 AM
Last Post: buran
  how to deal with problem of converting string to int usthbstar 1 2,032 Jan-05-2021, 01:33 PM
Last Post: perfringo
  Converting string to hex triplet menator01 4 4,442 Aug-03-2020, 01:00 PM
Last Post: deanhystad
  converting string object inside a list into an intiger bwdu 4 2,700 Mar-31-2020, 10:36 AM
Last Post: buran
  Converting query string as a condition for filter data. shah_entrance 1 1,833 Jan-14-2020, 09:22 AM
Last Post: perfringo
  Converting a patterned string or text into excel table soup1987 1 2,164 Oct-03-2019, 01:37 AM
Last Post: Larz60+
  converting array to and from string in python 3.7.2 srm 5 6,295 Jul-03-2019, 01:11 PM
Last Post: snippsat
  Trouble converting JSON String to Dictionary RBeck22 7 5,269 Mar-28-2019, 12:12 PM
Last Post: RBeck22
  converting binary b'0x31303032\n' to "1002" string amygdalas 2 2,690 Nov-07-2018, 03:50 AM
Last Post: amygdalas

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020