Learning Python – Post 5: Converting Between Numbers and Strings

Learning Python – Post 5: Converting Between Numbers and Strings

One of the first things I wanted to get comfortable with in Python was how to convert between numbers and strings — especially when dealing with user input or formatting output.

Here are the core functions I practiced, with examples that helped it all click

float() – Convert to Decimal

Turns a number or numeric string into a floating point number.

a = 31
a = float(a)
print(a) # 31.0

int() – Convert to Whole Number

Removes the decimal part (does not round!).

a = 31.34
a = int(a)
print(a) # 31

str() – Convert to Text

Changes a number into a string — useful when combining with other text or checking length.

a = 1234
a = str(a)
print(a) # '1234'
print(len(a)) # 4

float() with a string

s = "3.14"
s = float(s)
print(s) # 3.14

int() with a string

s = "543"
s = int(s)
print(s) # 543

str() – Convert Number to Text

Turns any number into a string. Useful when you want to combine it with other text, or count its characters.

a = 1234
a = str(a)
print(a) # '1234'
print(len(a)) # 4

Now you can treat it like a regular piece of text.

Summary Table:

Summary Table:

What you wantUse thisExample
int → floatfloat(x)float(3)3.0
float → intint(x)int(4.99)4
number → stringstr(x)str(123)'123'
"123" → intint("123")123
"3.14" → floatfloat("3.14")3.14
"123" → string str("123")'123''123'

I find these small conversions show up constantly, even in beginner projects.
Understanding them early saves a lot of headaches later.

Leave a Reply

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