How To Convert Python String To Int? – POFTUT

How To Convert Python String To Int?

Python has different variable types. Integer and string types are the most used types. And the conversation between them is required most of the time. In this post, we will look howto convert string to int?

The string is character array-like text data. Following examples are all string

All of the above definitions are a string but they hold different or numeric characters. An integer is base type for python. An integer is actually an umbrella definition.

Convert Single String Value to Integer

We will convert a single string variable value into an integer by using int function provided python.

Type of the new variable can get with with type function.

Convert Single String Value to Integer

Convert Single String Value to Integer

Convert To Float

A string can be converted to float too. Float provides more precision on the numeric type. In this example, we will convert 1.5 into the float. As float provides floating numbers.

Convert To Float

Convert To Float

Check Converted Number Type

We have already seen how to check the converted number types. But I just wanted to show this explicitly. Python programming language provides type() function which is used to check given variable type and print as type. In this example, we will check the type of variable str which is a string.

Check Converted Number Type

Check Converted Number Type

LEARN MORE  How To Sort Python Dictionary By Value?

1 Response

  1. 21/05/2017

    […] Integer […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Enjoy this blog? Please spread the word :)