In Python, a string is just text wrapped in quotes, like "hello"
or "42"
. Sometimes, we work with numbers, lists, booleans (True
or False
), or even custom objects—and we need to turn them into strings. This process is called string conversion.
Why would you want to do that? Imagine you’re printing a message like "You are 10 years old"
. The number 10
isn’t a string, so Python needs help to turn it into one before it can glue the parts together. You might also want to save data in a file, display it on a screen, or build messages using values from your code.
This article will walk you through how to convert different types into strings in Python. No tricky stuff—just simple, clear ways to do it!
Using str()
– The Standard Way
The easiest way to turn something into a string in Python is by using the str()
function. You just wrap whatever you want to convert inside str()
and Python will return a string version of it.
Let’s see how it works with different types:
Integers (int
)
str(42) # "42"
Floating-point numbers (float
)
str(3.14) # "3.14"
Booleans (bool
)
str(True) # "True"
str(False) # "False"
Lists (list
)
str(['Python', 'Rust', 'Go']) # "['Python', 'Rust', 'Go']"
Dictionaries (dict
)
str({'name': 'Edward', 'age': 10}) # "{'name': 'Edward', 'age': 10}"
Tuples (tuple
)
str(('red', 'green', 'blue')) # "('red', 'green', 'blue')"
NoneType
str(None) # "None"
No matter what type you’re working with, str()
is usually the go-to choice when you just want a quick, simple string version of something.
Using f-strings
(Formatted String Literals)
Starting from Python 3.6, there’s a cool and simple way to turn things into strings using f-strings. You write an f before your string and then use curly braces {}
to put values inside the string.
It looks like this:
f"{value}"
Let’s try it with some different types:
Number
f"{3.14}" # "3.14"
Boolean
f"{True}" # "True"
List
f"{['Python', 'C++']}" # "['Python', 'C++']"
You can also mix text and values easily. For example:
name = "Lucia"
age = 12
print(f"My name is {name} and I am {age} years old.") # "My name is Lucia and I am 12 years old."
F-strings are great when you want to create messages with both text and values all in one go. They’re short, neat, and easy to read!
Using format()
Method
Another way to turn values into strings is by using the .format()
method. You write a string with curly braces {}
as placeholders, then call .format()
to fill them with values.
The basic style looks like this:
"{}".format(value)
Let’s see some examples with different types:
Number
"Pi is {}".format(3.14) # "Pi is 3.14"
Boolean
"The answer is {}".format(True) # "The answer is True"
List
"My favorite languages are {}".format(['Python', 'Java']) # "My favorite languages are ['Python', 'Java']"
Multiple values
"Name: {}, Age: {}".format("Edward", 10) # "Name: Edward, Age: 10"
The format()
method is handy when you want more control over the layout of your string, like choosing the order or adding extra formatting rules. It’s a bit older than f-strings but still very useful!
Using String Concatenation with +
Sometimes, you might want to build a longer string by sticking smaller parts together using the +
symbol. This is called string concatenation.
But here’s the thing: Python only lets you add strings together. If you try to add a string and a number (or something else), you’ll get an error.
This will cause an error:
"Age: " + 10 # TypeError!
Python says: “Hey, I don’t know how to add a string and a number!”
The fix: Convert the number to a string first:
"Age: " + str(10) # "Age: 10"
You can use this trick with any type:
"Status: " + str(True) # "Status: True"
"List: " + str([1, 2, 3]) # "List: [1, 2, 3]"
So remember—if you’re using +
with a string, make sure everything you add is also a string. If it’s not, just wrap it with str()
!
Converting Elements in a List to Strings
If you use str()
on a whole list, Python gives you a single string that looks like the list:
langs = ['Python', 'Java', 3]
print(str(langs)) # "['Python', 'Java', 3]"
But what if you want to turn each item in the list into a string, like this?
['Python', 'Java', '3']
You can do that with two simple methods:
Method 1: List Comprehension
This is like a mini loop inside square brackets:
[str(item) for item in ['Python', 'Java', 3]] # ['Python', 'Java', '3']
Method 2: Using map()
The map()
function applies str()
to every item in the list:
list(map(str, ['Python', 'Java', 3])) # ['Python', 'Java', '3']
Both ways work the same. Use whichever one feels easier to you!
Joining Strings from Other Types
In Python, you can glue strings together using .join()
. It takes a list of strings and joins them with something in between—like a comma, space, or dash.
Simple example with strings
langs = ['Python', 'Rust', 'Go']
print(", ".join(langs)) # "Python, Rust, Go"
But .join()
only works if everything in the list is a string. If the list has numbers or other types, you’ll get an error.
Example with numbers – use map(str, ...)
to fix it
numbers = [1, 2, 3]
print(", ".join(map(str, numbers))) # "1, 2, 3"
So if your list has other types, just turn each item into a string first using map(str, ...)
. Then you’re good to go!
Custom Class Conversion Using __str__()
When you create your own classes in Python, you can decide what they should look like when turned into strings. You do this by adding a special method called __str__()
(that’s two underscores before and after).
Let’s see an example:
class Animal:
def __str__(self):
return "I am an animal"
pet = Animal()
print(str(pet)) # "I am an animal"
When you use str(pet)
or print(pet)
, Python calls the __str__()
method behind the scenes. Without it, you’d just see something like <__main__.Animal object at 0x...>
, which isn’t very helpful.
So, if you want your objects to speak clearly when converted to strings, just give them a __str__()
!
Conclusion
Converting things to strings in Python is super useful, and now you know lots of ways to do it!
You learned how to use:
str()
– the basic and reliable wayf-strings
– great for mixing values with textformat()
– handy for controlling layout+
– for sticking strings together (just usestr()
when needed)map(str, ...)
– for changing many items to strings at once
Try these out with different types like numbers, booleans, lists, or even your own classes. See how Python turns them into words you can print, join, or store.