Convert numbers string to numbers in python

Convert a string to a number (int, float) in Python

In Python, you can convert a string str to an integer int and a floating point number float with int() and float() .

Use str() to convert an integer or floating point number to a string.

If you want to change the format of strings or numbers, use the format() function or the format() method of str .

You can also convert a list of strings to a list of numbers.

Convert a string to int : int()

int() converts a string to an integer int .

print(int('100')) print(type(int('100'))) # 100 # 

A string containing . or , raises an error.

# print(int('1.23')) # ValueError: invalid literal for int() with base 10: '1.23' # print(int('10,000')) # ValueError: invalid literal for int() with base 10: '10,000' 

To convert a comma-separated string, you can remove the commas by replacing them with an empty string » using replace() .

print(int('10,000'.replace(',', ''))) # 10000 

For more information on replace() , see the following article.

Convert a string to float : float()

float() converts a string to a floating point number float .

print(float('1.23')) print(type(float('1.23'))) # 1.23 # 

A string without the integer part can also be converted.

A string representing an integer is also converted to a floating point number float .

print(float('100')) print(type(float('100'))) # 100.0 # 

Convert binary, octal, and hexadecimal strings to int

If you pass a base number as the second argument of int() , the string will be treated as binary, octal, or hexadecimal, depending on the base.

print(int('100', 2)) print(int('100', 8)) print(int('100', 16)) # 4 # 64 # 256 

If the base is omitted, the string will be treated as decimal, as shown in previous examples.

print(int('100', 10)) print(int('100')) # 100 # 100 

If the base is 0 , it is converted based on the string’s prefix ( 0b , 0o , 0x or 0B , 0O , 0X ).

print(int('0b100', 0)) print(int('0o100', 0)) print(int('0x100', 0)) # 4 # 64 # 256 

Prefixes and hexadecimal letters can be written in either upper or lower case.

print(int('FF', 16)) print(int('ff', 16)) # 255 # 255 print(int('0xFF', 0)) print(int('0XFF', 0)) print(int('0xff', 0)) print(int('0Xff', 0)) # 255 # 255 # 255 # 255 

For more information on the interconversion of binary, octal, and hexadecimal strings to int , see the following article.

Convert exponential notation strings to float

You can convert a string in exponential notation to float with float() .

print(float('1.23e-4')) print(type(float('1.23e-4'))) # 0.000123 # print(float('1.23e4')) print(type(float('1.23e4'))) # 12300.0 # 

Источник

Python Convert String to Int – How to Cast a String in Python

Dionysia Lemonaki

Dionysia Lemonaki

Python Convert String to Int – How to Cast a String in Python

When you’re programming, you’ll often need to switch between data types.

The ability to convert one data type to another gives you great flexibility when working with information.

There are different built-in ways to convert, or cast, types in the Python programming language.

In this article, you’ll learn how to convert a string to an integer.

Data types in Python

Python supports a variety of data types.

Data types are used for specifying, representing, and categorizing the different kinds of data that exist and are used in computer programs.

Also, different operations are available with different types of data – one operation available in one data type is often not available in another.

One example of a data type is strings.

Strings are sequences of characters that are used for conveying textual information.

They are enclosed in single or double quotation marks, like so:

fave_phrase = "Hello world!" #Hello world! is a string,enclosed in double quotation marks 

Ints, or integers, are whole numbers.

They are used to represent numerical data, and you can do any mathematical operation (such as addition, subtraction, multiplication, and division) when working with integers.

Integers are not enclosed in single or double quotation marks.

fave_number = 7 #7 is an int #"7" would not be an int but a string, despite it being a number. #This is because of the quotation marks surrounding it 

Data type conversion

Sometimes when you’re storing data, or when you receive input from a user in one type, you’ll need to manipulate and perform different kinds of operations on that data.

Since each data type can be manipulated in different ways, this often means that you’ll need to convert it.

Converting one data type to another is also called type casting or type conversion. Many languages offer built-in cast operators to do just that – to explicitly convert one type to another.

How to convert a string to an integer in Python

To convert, or cast, a string to an integer in Python, you use the int() built-in function.

The function takes in as a parameter the initial string you want to convert, and returns the integer equivalent of the value you passed.

The general syntax looks something like this: int(«str») .

Let’s take the following example, where there is the string version of a number:

#string version of the number 7 print("7") #check the data type with type() method print(type("7")) #output #7 #

To convert the string version of the number to the integer equivalent, you use the int() function, like so:

#convert string to int data type print(int("7")) #check the data type with type() method print(type(int("7"))) #output #7 #

A practical example of converting a string to an int

Say you want to calculate the age of a user. You do this by receiving input from them. That input will always be in string format.

So, even if they type in a number, that number will be of .

If you want to then perform mathematical operations on that input, such as subtracting that input from another number, you will get an error because you can’t carry out mathematical operations on strings.

Check out the example below to see this in action:

current_year = 2021 #ask user to input their year of birth user_birth_year_input = input("What year were you born? ") #subtract the year the user filled in from the current year user_age = current_year - user_birth_year_input print(user_age) #output #What year were you born? 1993 #Traceback (most recent call last): # File "demo.py", line 9, in # user_age = current_year - user_birth_year_input #TypeError: unsupported operand type(s) for -: 'int' and 'str' 

The error mentions that subtraction can’t be performed between an int and a string.

You can check the data type of the input by using the type() method:

current_year = 2021 #ask user to input their year of birth user_birth_year_input = input("What year were you born? ") print(type(user_birth_year_input)) #output #What year were you born? 1993 #

The way around this and to avoid errors is to convert the user input to an integer and store it inside a new variable:

current_year = 2021 #ask user to input their year of birth user_birth_year_input = input("What year were you born? ") #convert the raw user input to an int using the int() function and store in new variable user_birth_year = int(user_birth_year_input) #subtract the converted user input from the current year user_age = current_year - user_birth_year print(user_age) #output #What year were you born? 1993 #28 

Conclusion

And there you have it — you now know how to convert strings to integers in Python!

If you want to learn more about the Python programming language, freeCodeCamp has a Python Certification to get you started.

You’ll start with the fundamentals and progress to more advance topics like data structures and relational databases. In the end, you’ll build five projects to practice what you learned.

Thanks for reading and happy coding!

Источник

Convert a string to a number (int, float) in Python

In Python, you can convert a string str to an integer int and a floating point number float with int() and float() .

Use str() to convert an integer or floating point number to a string.

If you want to change the format of strings or numbers, use the format() function or the format() method of str .

You can also convert a list of strings to a list of numbers.

Convert a string to int : int()

int() converts a string to an integer int .

print(int('100')) print(type(int('100'))) # 100 # 

A string containing . or , raises an error.

# print(int('1.23')) # ValueError: invalid literal for int() with base 10: '1.23' # print(int('10,000')) # ValueError: invalid literal for int() with base 10: '10,000' 

To convert a comma-separated string, you can remove the commas by replacing them with an empty string » using replace() .

print(int('10,000'.replace(',', ''))) # 10000 

For more information on replace() , see the following article.

Convert a string to float : float()

float() converts a string to a floating point number float .

print(float('1.23')) print(type(float('1.23'))) # 1.23 # 

A string without the integer part can also be converted.

A string representing an integer is also converted to a floating point number float .

print(float('100')) print(type(float('100'))) # 100.0 # 

Convert binary, octal, and hexadecimal strings to int

If you pass a base number as the second argument of int() , the string will be treated as binary, octal, or hexadecimal, depending on the base.

print(int('100', 2)) print(int('100', 8)) print(int('100', 16)) # 4 # 64 # 256 

If the base is omitted, the string will be treated as decimal, as shown in previous examples.

print(int('100', 10)) print(int('100')) # 100 # 100 

If the base is 0 , it is converted based on the string’s prefix ( 0b , 0o , 0x or 0B , 0O , 0X ).

print(int('0b100', 0)) print(int('0o100', 0)) print(int('0x100', 0)) # 4 # 64 # 256 

Prefixes and hexadecimal letters can be written in either upper or lower case.

print(int('FF', 16)) print(int('ff', 16)) # 255 # 255 print(int('0xFF', 0)) print(int('0XFF', 0)) print(int('0xff', 0)) print(int('0Xff', 0)) # 255 # 255 # 255 # 255 

For more information on the interconversion of binary, octal, and hexadecimal strings to int , see the following article.

Convert exponential notation strings to float

You can convert a string in exponential notation to float with float() .

print(float('1.23e-4')) print(type(float('1.23e-4'))) # 0.000123 # print(float('1.23e4')) print(type(float('1.23e4'))) # 12300.0 # 

Источник

Читайте также:  Обнулить все стили css
Оцените статью