Convert strings to int or float in Python 3?

35,300

Solution 1

You have to convert the integer into a string:

print('2 + ' + str(integer) + ' = ' + str(rslt))

Or pass it as an argument to print and print will do it for you:

print('2 +', integer, '=', rslt)

I would do it using string formatting:

print('2 + {} = {}'.format(integer, rslt))

Solution 2

In Python 3.x - input is the equivalent of Python 2.x's raw_input...

You should be using string formatting for this - and perform some error checking:

try:
    integer = int(input('something: '))
    print('2 + {} = {}'.format(integer, integer + 2))
except ValueError as e:
    print("ooops - you didn't enter something I could make an int of...")

Another option - that looks a bit convoluted is to allow the interpreter to take its best guess at the value, then raise something that isn't int or float:

from ast import literal_eval

try:
    value = literal_eval(input('test: '))
    if not isinstance(value, (int, float)):
        raise ValueError
    print value + 2
except ValueError as e:
    print('oooops - not int or float')

This allows a bit more flexibility if you wanted complex numbers or lists or tuples as input for instance...

Solution 3

Your problem is not with converting the input to an integer. The problem is that when you write ' = ' + rslt you are trying to add an integer to a string, and you can't do that.

You have a few options. You can convert integer and rslt back into strings to add them to the rest of your string:

print('2 + ' + str(integer) + ' = ' + str(rslt))

Or you could just print multiple things:

print('2 + ', integer, ' = ', rslt)

Or use string formatting:

print('2 + {0} = {1}'.format(integer, rslt))
Share:
35,300
Rabcor
Author by

Rabcor

So far i've been trying to learn C#, HTML/CSS, SQL in school and Python in my free time. So far pretty good.

Updated on November 19, 2020

Comments

  • Rabcor
    Rabcor over 3 years
     integer = input("Number: ")
     rslt = int(integer)+2
     print('2 + ' + integer + ' = ' + rslt)
     double = input("Point Number: ")
     print('2.5 + ' +double+' = ' +(float(double)+2.5))
    

    Gives me

    Traceback (most recent call last):
      File "C:\...", line 13, in <module>
        print('2 + ' + integer + ' = ' + rslt)
    TypeError: Can't convert 'int' object to str implicitly
    

    I'm fairly new to programming and my background is mostly just the basics of C# so far. I wanted to try to learn Python through doing all my C# school projects on Python. I'm used to the simple syntax of C# which would look something like this:

    int integer = Convert.ToInt32(Console.ReadLine())
    

    or

    double double = Convert.ToDouble(Console.ReadLine())
    

    Which takes a user input string and converts it to what I specified.

    I think I read py2.x has a command called raw_input that works a bit better than the input command of py3.x in this regard.

    I was trying to find myself a similar format as the one I'm used to in C# to use in Python, but it's proving surprisingly hard just to find a method to convert the user input string into an integer after all this googling and trying everything I could think of (and that I found on google) I decided it was time to ask. Can you help?

  • Rabcor
    Rabcor about 11 years
    Thank you! that answered everything for me.
  • Rabcor
    Rabcor about 11 years
    Thank you! that answered everything for me aswell :)
  • Jon Clements
    Jon Clements about 11 years
    @user2115045 are you sure? (edit: yes - I didn't paste the last ) in there...¬)