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
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. This page is licensed under the Python Software Foundation License Version 2. Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
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 str in python returns the string object value (string version) of the given object or number. If we do not give any parameter in the method, it will return an empty string. Let us take a few examples to understand the str method in python in a better way.
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)
Scaler
scaler.com › home › topics › what is python __str__?
What is Python __str__? | Scaler Topics
June 11, 2024 - This method is also called as magic method or dunder method. The python __str__ method returns the string representation of the object. The same method is also called when the str() or print() function is invoked on an object. We'll see examples of such instances in the article below.
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
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.
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 returns a str object representing the given object.
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.
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.