🌐
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
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-str-function
Python str() function - GeeksforGeeks
July 15, 2025 - It can be used to convert various data types into strings, which can then be used for printing, concatenation, and formatting. Let’s take a simple example to converting an Integer to string.
🌐
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.
🌐
W3Schools
w3schools.com › python › python_strings.asp
Python Strings
To check if a certain phrase or character is present in a string, we can use the keyword in. ... txt = "The best things in life are free!" if "free" in txt: print("Yes, 'free' is present.") Try it Yourself » · Learn more about If statements in our Python If...Else chapter.
🌐
Real Python
realpython.com › ref › builtin-types › str
str | Python’s Built-in Data Types – Real Python
In this example, strings are used to format and present user data in a readable format. ... Work with Python strings using a rich set of operators and functions: string literals, the str() function, and the methods you'll use most.
🌐
Google
developers.google.com › google for education › python › python strings
Python Strings | Python Education | Google for Developers
Since strings can't be changed, we construct *new* strings as we go to represent computed values. So for example the expression ('hello' + 'there') takes in the 2 strings 'hello' and 'there' and builds a new string 'hellothere'.
🌐
DigitalOcean
digitalocean.com › community › tutorials › python-str-repr-functions
How To Use the __str__() and __repr__() Methods in Python | DigitalOcean
Learn Python’s str() and repr() methods in depth. Explore examples, best practices, and real-world tips for clarity and debugging.
Find elsewhere
🌐
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.
🌐
Spark By {Examples}
sparkbyexamples.com › home › python › python str() method
Python str() Method - Spark By {Examples}
May 31, 2024 - The Python str() method is used to convert an object into a string representation. It is a built-in function that can be used to convert objects of
🌐
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 :)

🌐
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.
🌐
Intellipaat
intellipaat.com › home › blog › python str() function
Python str() Function: Overview, Syntax and Examples
November 11, 2025 - Use the Python str() function to convert an integer to a string. For example: str(123) returns ‘123’.