W3Schools
w3schools.com โบ python โบ ref_func_str.asp
Python str() Function
The str() function converts the specified value into a string. ... Coding fundamentals as bite-sized lessons and challenges. ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: ...
In what cases do you use the str() function?
str() is used to generate a 'human readable string expression' for an object in python. Say for example , we have the following class in python: class Person: def __init__(self,name,age): self.name = name self.age = age print(Person('Anon',30)) This prints <__main__.Person object at 0x7f12b3fc7400> Which is a pointer to the memory address , not very helpful to comprehend. On the other hand , class Person: def __init__(self,name,age): self.name = name self.age = age def __str__(self): return f"Name:{name} Age:{age}" print(Person('Anon',30)) will print Name:Anon Age:30 which is more human readable ( kinda). The same thing happens with int - it has an inbuilt __str__ method which parses the input and converts it into a human readable string ( using ASCII/UTF-8 conversion or whatever). If you are still interested , I'd recommend going and reading up on __repr__ , which is similar to __str__ but is intended to be used via debugging rather than returning a string representation. Tbf , you can get around this using fstrings .... More on reddit.com
What does __str__ method do?
Yeah, it's just so you can print it. If you try to print a variable that is not a string it won't know what to do, so when you make a __str__ method it's like saying "If I tell you to print this variable, this is how I want you to do that" More on reddit.com
03:07
How and When to Use .__str__() (Video) โ Real Python
04:58
Class __str__ method - Python for Beginners - YouTube
03:13
__str__ Method | Python Tutorial - YouTube
05:02
Python built-in function: str() - YouTube
01:44
How to use str function in Python | Python functions made easy ...
Programiz
programiz.com โบ python-programming โบ methods โบ built-in โบ str
Python str() (With Examples)
The str() function returns the string version of the given object. In this tutorial, we will learn about Python str() in detail with the help of examples.
Reddit
reddit.com โบ r/learnpython โบ in what cases do you use the str() function?
r/learnpython on Reddit: In what cases do you use the str() function?
July 16, 2021 -
Title. I know you use it for integers, but in what context? Also, is it used for anything else?
Top answer 1 of 7
9
str() is used to generate a 'human readable string expression' for an object in python. Say for example , we have the following class in python: class Person: def __init__(self,name,age): self.name = name self.age = age print(Person('Anon',30)) This prints <__main__.Person object at 0x7f12b3fc7400> Which is a pointer to the memory address , not very helpful to comprehend. On the other hand , class Person: def __init__(self,name,age): self.name = name self.age = age def __str__(self): return f"Name:{name} Age:{age}" print(Person('Anon',30)) will print Name:Anon Age:30 which is more human readable ( kinda). The same thing happens with int - it has an inbuilt __str__ method which parses the input and converts it into a human readable string ( using ASCII/UTF-8 conversion or whatever). If you are still interested , I'd recommend going and reading up on __repr__ , which is similar to __str__ but is intended to be used via debugging rather than returning a string representation. Tbf , you can get around this using fstrings ....
2 of 7
4
If you want to convert anything to a string. Any integers, floats, lists, any object that implements __str__() really. I believe when you print something, it converts it to a string. So if you want a string, thats what str() is for
Stanford CS
cs.stanford.edu โบ people โบ nick โบ py โบ python-string.html
Python Strings
Similarly, if the start index is greater or equal to the end index, the slice is just the empty string. >>> s = 'Python' >>> s[2:999] 'thon' >>> s[3:2] # zero chars ''
Python Reference
python-reference.readthedocs.io โบ en โบ latest โบ docs โบ str
str โ Python Reference (The Right Way) 0.1 documentation
Returns a string containing a printable representation of an object.
Codecademy
codecademy.com โบ docs โบ python โบ dunder methods โบ __str__()
Python | Dunder Methods | __str__() | Codecademy
April 24, 2025 - The __str__() dunder method, also known as a magic method, returns a human-readable string representation of a class object. It can be called with the built-in str() and print() functions.
Python
docs.python.org โบ 3 โบ library โบ string.html
string โ Common string operations
Split the argument into words using str.split(), capitalize each word using str.capitalize(), and join the capitalized words using str.join(). If the optional second argument sep is absent or None, runs of whitespace characters are replaced by a single space and leading and trailing whitespace are removed, otherwise sep is used to split and join the words. ... ยฉ Copyright 2001 Python Software Foundation.
DataCamp
datacamp.com โบ tutorial โบ python-string-tutorial
Python String Tutorial | DataCamp
January 18, 2018 - String is a collection of alphabets, words or other characters. It is one of the primitive data structures and are the building blocks for data manipulation. Python has a built-in string class named str. Python strings are "immutable" which means they cannot be changed after they are created.
Reddit
reddit.com โบ r/learnpython โบ what does __str__ method do?
r/learnpython on Reddit: What does __str__ method do?
November 14, 2020 -
I'm still a beginner in python and my English is not really good, so I may have a little bit of understanding the explanation given in my course.
So I have this code:
class Apple:
def __init__ (self,color,flavor):
self.color=color
self.flavor=flavor
def __str__(self):
return "This apple is {} and its flavor is {}".format(self.name,self.flavor)
jonagold=Apple("red","sweet")
print(jonagold)The only purpose I see in using the __str__ method, I don't have to write the method in the print () line. If I changed the __str__ method with:
def say(self):
return "This apple is {} and its flavor is {}".format(self.name,self.flavor)I would just need to type
print(jonagold.say())
And I would get the same answer. What are the other purpose of using __str__? In what situation would I need to use the __str__?
Thank you :)
Top answer 1 of 4
4
Yeah, it's just so you can print it. If you try to print a variable that is not a string it won't know what to do, so when you make a __str__ method it's like saying "If I tell you to print this variable, this is how I want you to do that"
2 of 4
2
From what I understand __str__ defines how an object of the class will be converted to a string. So like when youโre printing or if you did str(jonagold) Theoretically you could make your own method instead of using __str__ but itโs sort of pointless and isnโt as nice. Like would you rather do print(jonagold) or print(jonagold.say()) ? The second one has more ways it can go wrong especially if someone else is trying to use your class
Python-future
python-future.org โบ str_object.html
str โ Python-Future documentation
The str object in Python 3 is quite similar but not identical to the Python 2 unicode object.
Codecademy
codecademy.com โบ docs โบ python โบ built-in functions โบ str()
Python | Built-in Functions | str() | Codecademy
January 1, 2022 - Takes in a value that can be converted into a string, and returns a copy of the value in the string datatype. If nothing is passed into str(), then an empty string is returned. ... Looking for an introduction to the theory behind programming?
Educative
educative.io โบ answers โบ what-is-the-str-method-in-python
What is the __str__ method in Python?
The __str__ method in Python represents class objects as a string. It can be used as a debugging tool when the members of a class need to be checked.