Python for Beginners: Turning Numbers into Text (String Conversion)

Converting to Text with python built in str() Function

Its simple and easy

We can convert number to string like this . here x=10 , where the value of x is a number when we write x=str(x) it becomes a string. Thats all!

x=10

x=str(x)

So we can convert number to string with str()



number = 2  # For example our number is 2

# You can  Check the data type of 'number' with type()
print(type(number))  # Output:  

# We added number+ number so we can show the simple calculation. To show the difference between result with number and string
print(number + number)  # Output: 4 

# NOW CONVERT 'NUMBER' FROM AN INTEGER TO A STRING
number = str(number)  

# Check the new data type As it successfully converted to string
print(type(number))  # Output: 

# And now if we ad number+number it will not calculate instead it will place the character beside the character 
print(number + number)  # Output: 22





Also we can convert string to number! with int() and float()




number='2'  # This time we added the number inside quotation. so it becomes string
print(number)

print(number+number)
# It will print 22 


#Use int() to integer and float() to convert to float
number= int(number)

print(number+number)
# it will print 4 as it converted to integer number




Published by

mashiurrahman99

Programmer