Programiz
programiz.com › python-programming › methods › built-in › str
Python str() (With Examples)
# convert a utf-8 byte object to ... byte 0xc3 in position 4: ordinal not in range(128) In the first example, we have created a byte object b with the string 'pythön' and the encoding utf-8....
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: sales@w3schools.com · If you want to report an error, or if you want to make a suggestion, send us an e-mail: help@w3schools.com · HTML Tutorial CSS Tutorial JavaScript Tutorial How To Tutorial SQL Tutorial Python Tutorial W3.CSS Tutorial Bootstrap Tutorial PHP Tutorial Java Tutorial C++ Tutorial jQuery Tutorial
05:02
Python built-in function: str() - YouTube
04:58
Class __str__ method - Python for Beginners - YouTube
03:13
__str__ Method | Python Tutorial - YouTube
01:44
How to use str function in Python | Python functions made easy ...
08:07
Python Tutorial #8: String Basics 🐍 - YouTube
05:28
Python data type: str - YouTube
Python
docs.python.org › 3 › library › string.html
string — Common string operations
If it is an integer, it represents the index of the positional argument in args; if it is a string, then it represents a named argument in kwargs. The args parameter is set to the list of positional arguments to vformat(), and the kwargs parameter is set to the dictionary of keyword arguments. For compound field names, these functions are only called for the first component of the field name; subsequent components are handled through normal attribute and indexing operations. So for example, the field expression ‘0.name’ would cause get_value() to be called with a key argument of 0.
Codecademy
codecademy.com › docs › python › dunder methods › __str__()
Python | Dunder Methods | __str__() | Codecademy
April 24, 2025 - In this example, the __str__() method formats the student’s information into a human-readable string. When the object is displayed or converted to a string, Python automatically calls the __str__() method.
Scaler
scaler.com › home › topics › str() in python
Python str() Function (With Examples) - Scaler Topics
March 22, 2024 - The conversion of one data type into the other data type is known as type conversion or type casting in python. Let us convert a number into a string object using the method, str in python.
Scaler
scaler.com › home › topics › what is python __str__?
What is Python __str__? | Scaler Topics
June 11, 2024 - The output of the above python code snippet is shown below: The above example shows the default implementation of the __str__ method. In the above example neither __str__ nor __repr__ methods are defined. Therefore, calling the __str__ method calls the default __repr__ method as explained above in the article.
Tutorialspoint
tutorialspoint.com › python › python-str-function.htm
Python str() Function
complex_num = complex(2, 3) str_complex = str(complex_num) print('The string value obtained is:',str_complex)
TechBeamers
techbeamers.com › str-function-in-python
Python str() Function Tutorial - TechBeamers
November 30, 2025 - # Example: Using escape sequences with the str function escaped_str = str("This is a line.\nThis is a new line.\tThis is a tab.") print(escaped_str) The str function is not only about converting other Python data types to strings but can also turn back converted strings back to their original state.
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
Educative
educative.io › answers › what-is-the-str-method-in-python
What is the __str__ method in Python?
In the first example we saw that when __str__ is not defined it automatically calls the __repr__ method. Therefore, the output of all the functions - __str__, str(), and __repr__ - are the same. Moreover, the __repr__ method does not necessarily need to return a string.
Dive into Python
diveintopython.org › home › functions & methods › built-in functions › str()
str() in Python - Built-In Functions with Examples
The str() function in Python is a built-in function that converts the specified value into a string.
Dataquest
dataquest.io › home › blog › python strings: an in-depth tutorial (55+ code examples)
Python Strings: An In-Depth Tutorial (55+ Code Examples)
April 24, 2025 - We can declare a Python string using a single quote, a double quote, a triple quote, or the str() function. The following code snippet shows how to declare a string in Python: # A single quote string single_quote = 'a' # This is an example of a character in other programming languages.