|
October 3, 2018
|

Python Convert String to Int

Converting Data Types in Python 3

 

Data types, in Python 3, like in the most programming languages, are used to define the type of values you can assign to one particular entity.

In order to achieve your goals when creating a complex software or a short script, you may need to convert one data type into another. Most likely, this is the reason you are here.

Let’s go through the most important data types in Python 3:

 

Numbers can be integers (5 and 7), floats (2.1 and 6.4), fractions (3/6 and 5/15), or even complex numbers.

Strings are sequences of Unicode characters, like a HTML document.

Lists are ordered sequences of values.

Tuples are ordered, immutable sequences of values.

Sets are unordered bags of values.

Dictionaries are unordered bags of key-value pairs.

Bytes and byte arrays, like a jpeg image file.

Booleans are only True or False.

 

Where it makes sense, some data type can be converted into anther ones.

 

Numbers

When it comes to numbers, we’ll talk about 2 types: integers and floats. Python 3 has an embedded to easily convert integers into floats and the other way around.

 

float(42) – this will convert the integer 42 into the float 42.0.

***

i = 42

print(float(i))

Output:

42.0

***

 

int(32.0) – this will convert the float 32.0 into the integer 32.

***

x = 32.0

print(int(x))

Output:

32

***

Important: The int function does not round up to the closest integer, it just removes everything after the period.

***

z = 37.9

print(int(z))

Output:

37

***

 

A big change in Python 3 vs Python 2 is that when doing a division, you will get a float as a result. In Python 2 you get an int.

***

b = 7 / 2

print(b)

Output:

3.5

***

 

Strings

If you want python convert string to int (integer) then this can be done very easily.

A string can contain any character – numbers, letters, symbols, etc.

You can define a string by placing its value between quotes:

***

s = ‘proxy’

print(s)

Output:

proxy

***

 

The function str converts a number or a the value of a variable into a string when converting a string to int.

***

print(str(8))

Output:

8

 

f = 8

print(str(f))

Output:

8

***

A string can not be used in mathematical operations. Example:

***

person = “John”

money = 20

currency = dollars

print(person + “ has ” + money + currency)

– will generate an error:

Output:

TypeError: Can’t convert ‘int’ object to str implicitly

– while using the str function:

person = “John”

money = 20

currency = dollars

print(person + “ has ” + str(money) + currency)

– will echo

Output:

John has 20 dollars

***

 

Converting a float to a string will keep the exact text (value) of the float:

***

person = “Diana”

height = 5.6

print(person + “ is ” + str(height) + “ feet ” + “ tall ”)

Output:

Diana is 5.6 feet tall

***

 

 

The same goes for converting string to integers or floats:

***

person = “Tom”

wallet1 = “11.5”

wallet2 = “23.3”

total = float(wallet1) + float(wallet2)

currency = “dollars”

print(person + “ has two wallets. The total amount of money is “ + total)

Output:

Tom has two wallets. The total amount of money is 34.8

***

Important! If you try to convert a decimal value in a string to an integer using the int function, you will receive an error.

 

Lists and tuples

 

Both lists and tuples are ordered sequences of values. The difference between them is that tuples have the immutable attribute. This means the order of the elements can NOT be changed.

A tuple is presented exactly as a list, except that the whole set of elements is between parentheses, instead of square brackets.

list:

services = [‘proxy’, ‘socks5’, ‘proxies’, ‘socks’]

tuple:

services = (‘proxy’, ‘socks5’, ‘proxies’, ‘socks’)

so, converting a list to a tuple would be:

***

services = [‘proxy’, ‘socks5’, ‘proxies’, ‘socks’]

print(tuple(services))

Output:

(‘proxy’, ‘socks5’, ‘proxies’, ‘socks’) #notice the parentheses

***

 

Strings can also be converted to tuples, due to the fact that we can iterate through strings:

***

print(tuple(‘proxy’))

Output:

(‘p’, ’r’, ’o’, ‘x’, ‘y’)

***

 

Based on the same rule, integers and floats can NOT be converted to tuples, as you can not iterate through them.

 

The same methods are used for converting to lists.

From tuples:

***

services = (‘proxy’, ‘socks5’, ‘proxies’, ‘socks’)

print(list(services))

Output:

[‘proxy’, ‘socks5’, ‘proxies’, ‘socks’]

***

 

From strings:

***

print(list[‘socks5’])

Output:

[‘s’, ’o’, ’c’, ‘k’, ‘s’, ‘5’]

***

 

Being able to convert between data types is a great advantage when writing software, especially when the programming language has sophisticated, but easy to use embedded methods which can spare you from writing multiple code lines.

LEAVE A COMMENT

Residential VPN is fully sold out!

Notify me when more slots are available

We expect more slots to be available in the next 2 weeks.
NOTIFY ME PLEASE