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
#1
I'm trying to convert strings to numbers, to multiply both numbers, when i print it out.. It still warns me its 2 strings
num = input("Enter a number to CUBE: ")

def cube():

    print(int(num * num * num))


cube()
Reply
#2
int(num * num * num) does the operation inside the the parenthesis before converting to int. You can do this:
print((int(num)**3)
# or
num = int(num)
print(num * num * num)
Reply
#3
(Jul-07-2021, 03:57 PM)deanhystad Wrote: int(num * num * num) does the operation inside the the parenthesis before converting to int. You can do this:
print((int(num)**3)
# or
num = int(num)
print(num * num * num)


Oh ok i understand. But 1 question. First try i did this i for example put 3 to cube, and it splitted them into 333333333 instead of 27, why?
num = input("Enter a number to CUBE: ")
result = int(num)


def cube():
    print(int(num * int(num) * int(num)))


cube()
Reply
#4
You don't convert first num to integer in function.
You convert with result,but never use it.
num = input("Enter a number to CUBE: ")
result = int(num)

def cube():
    print(result * result * result)

cube()
But should not write as this function should take argument in like this,can also do convert on input().
Then it will be like this,now it clear that cube need a argument num in.
def cube(num):
    print(num * num * num)

if __name__ == '__main__':
    num = int(input("Enter a number to CUBE: "))
    cube(num)
Reply
#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
#6
Why cant i don like this?

 int(num1)
 int(num2)
 print(num1 * num2)
Reply
#7
(Jul-07-2021, 07:59 PM)dedesssse Wrote: Why cant i don like this?
Not storing the integer in a variables,so line 1,2 dos nothing.
num1 = '5'
num2 = '10'
num1 = int(num1)
num2  = int(num2)
print(num1 * num2)
Output:
50
Try to understand solution that's given.
Reply
#8
int(num) is a function that takes a string (num) and returns an integer. The function does not change the argument. For example:
string = '5'
num = int(string)
print(string, type(string), num, type(num))
Output:
5 <class 'str'> 5 <class 'int'>
string is still a str after calling int(string). It is unchanged.

This is really basic stuff. I think you would benefit from doing some online tutorials. After you learn more about functions the reason why int(num) does not convert num from a string to an int will be obvious.
Reply


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