Open In App

Convert Strings to Numbers and Numbers to Strings in Python

Last Updated : 12 Jan, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In Python, strings or numbers can be converted to a number of strings using various inbuilt functions like str(), int(), float(), etc. Let’s see how to use each of them. 

Example 1: Converting a Python String to an int:

Python3




# code
# gfg contains string 10
gfg = "10"
 
# using the int(), string is auto converted to int
print(int(gfg)+20)


Output

30

Example 2: Converting a Python String to float:

Python3




# code
gfg = "10"
 
# float(gfg) gives 10.0
print(float(gfg)+2.0)


Output

12.0

Example 3: Converting a Python int to a String:

This is achieved by using str() function as shown below

Python3




# code
gfg = 100
 
# str(gfg) gives '100'
print(str(gfg)+" is a 3 digit number")
 
# concatenation is performed between them
print(str(gfg)+"200")


Output

100 is a 3 digit number
100200

Example 4: Converting a Python float to a String

This is achieved by using str() function as shown below

Python3




# code
gfg = 20.0
 
# str(gfg) becomes '20.0'
print(str(gfg)+"is now a string")
 
# no addition is performed. concatenated output
print(str(gfg)+"30.0")


Output

20.0is now a string
20.030.0

Example 5: Converting python int to string using format() function 

Python3




# python program to convert integer number
# to string using format() function
intnum = 20
print("string is {} ".format(intnum))
print(type("string is {} ".format(intnum)))


Output

string is 20 
<class 'str'>

Example 5: Converting python float to string using format() function 

Python3




# python program to convert float integer number
# to string using format() function
floatnum = 20.00
print("string is {} ".format(floatnum))
print(type("string is {} ".format(floatnum)))


Output

string is 20.0 
<class 'str'>

Example 6: Converting python string to int using eval() function 

Python3




# converting python string to
# int using eval() function
a = "100"
print(eval(a)+12)


Output

112

Example 7: Converting python string to float using eval() function  

Python3




# converting python string to
# float using eval() function
a = "100.00"
print(eval(a)+12)


Output

112.0


Similar Reads

Python | Convert list of strings and characters to list of characters
Sometimes we come forward to the problem in which we receive a list that consists of strings and characters mixed and the task we need to perform is converting that mixed list to a list consisting entirely of characters. Let's discuss certain ways in which this is achieved. Method #1 : Using List comprehension In this method, we just consider each
6 min read
Python | Convert case of elements in a list of strings
Given a list of strings, write a Python program to convert all strings from lowercase/uppercase to uppercase/lowercase. Input : ['GeEk', 'FOR', 'gEEKS'] Output: ['geek', 'for', 'geeks'] Input : ['fun', 'Foo', 'BaR'] Output: ['FUN', 'FOO', 'BAR'] Method #1: Convert Uppercase to Lowercase using map function C/C++ Code # Python code to convert all str
3 min read
Python | Ways to convert array of strings to array of floats
Sometimes in a competitive coding environment, we get input in some other datatypes and we need to convert them into other forms this problem is the same as when we have an input in the form of string and we need to convert it into floats. Let's discuss a few ways to convert an array of strings in Python to an array of floats. Input: ['1.1' '1.5' '
5 min read
Python | Convert list of tuples to list of strings
The interconversion between datatypes is a quite useful utility and many articles have been written to perform the same. This article discusses the interconversion between a tuple of characters to individual strings. This kind of interconversion is useful in Machine Learning in which we need to give the input to train the model in a specific format
4 min read
Convert list of strings to list of tuples in Python
Sometimes we deal with different types of data types and we require to inter-convert from one data type to another hence interconversion is always a useful tool to have knowledge. This article deals with the converse case. Let's discuss certain ways in which this can be done in Python. Method 1: Convert a list of strings to a list of tuples using t
5 min read
Python | Convert list of strings to space separated string
Given a list of strings, write a Python program to convert the given list of strings into a space-separated string. Examples: Input : ['geeks', 'for', 'geeks'] Output : geeks for geeks Input : ['Python', 'Language'] Output : Python Language Approach #1 : Python string translate() The string translate() method returns a string where each character i
4 min read
Python | Convert List of lists to list of Strings
Interconversion of data is very popular nowadays and has many applications. In this scenario, we can have a problem in which we need to convert a list of lists, i.e matrix into list of strings. Let's discuss certain ways in which this task can be performed. Method #1 : Using list comprehension + join() The combination of above functionalities can b
4 min read
Python - Convert Strings to Character Matrix
Sometimes, while dealing with String lists, we can have a problem in which we need to convert the Strings in list to separate character list. Overall converting into Matrix. This can have multiple applications in data science domain in which we deal with lots of data. Lets discuss certain ways in which this task can be performed. Method #1 : Using
3 min read
Python - Convert Strings to Uppercase in Dictionary value lists
Given dictionary with values list, convert all the strings to upper case. Input : {"Gfg" : ["ab", "cd"], "Best" : ["gh"], "is" : ["kl"]} Output : {'Gfg': ['AB', 'CD'], 'Best': ['GH'], 'is': ['KL']} Explanation : All value lists strings are converted to upper case. Input : {"Gfg" : ["ab", "cd", "Ef"]} Output : {'Gfg': ['AB', 'CD', "EF"]} Explanation
8 min read
Python program to convert a list of strings with a delimiter to a list of tuple
Given a List containing strings with a particular delimiter. The task is to remove the delimiter and convert the string to the list of tuple. Examples: Input : test_list = ["1-2", "3-4-8-9"], K = "-" Output : [(1, 2), (3, 4, 8, 9)] Explanation : After splitting, 1-2 =&gt; (1, 2). Input : test_list = ["1*2", "3*4*8*9"], K = "*" Output : [(1, 2), (3,
7 min read
three90RightbarBannerImg