Python str() Function
str() is one of the most commonly used built-in functions in Python; it converts data of other types into a string.
Strings are one of the most commonly used data types in Python;str() the function helps us convert numbers, lists, tuples and so on into strings, making output and concatenation easier.
Word meaning: str Is stringis short for (string).
Basic syntax and parameters
str() is a built-in function and can be called directly.
Syntax format
str(object)
str(object, encoding)
str(object, errors)
Parameter Description
- Parameter object:
- Type: any object
- Description: the object to convert into a string.
- Parameter encoding(optional):
- Type: string
- Description: the encoding format (such as 'utf-8' or 'gbk').
- Parameter errors(optional):
- Type: string
- Description: the error handling scheme, defaulting to 'strict' (raise an exception). Other options: 'ignore', 'replace'.
Function description
- Return Value: Returns a string object.
- Default behavior: Called without arguments it returns an empty string
""。
Example
Let's master it through examples str() through a series of examples from simple to complex.
Example 1: basic usage - converting various types
Example
Convert an integer to a string
(
str
(
123
)
)
Output: 123
(
type
(
str
(
123
)
)
)
Output:
Convert a float to a string
(
str
(
3.14159
)
)
Output: 3.14159
Convert a Boolean to a string
(
str
(
True
)
)
Output: True
(
str
(
False
)
)
Output: False
Convert a list to a string
(
str
(
[
1
,
2
,
3
]
)
)
Output: [1, 2, 3]
Convert a tuple to a string
(
str
(
(
1
,
2
,
3
)
)
)
Output: (1, 2, 3)
Convert a dictionary to a string
(
str
(
{
"name"
:
"Tom"
,
"age"
:
20
}
)
)
Output: {'name': 'Tom', 'age': 20}
No arguments
(
str
(
)
)
Output: (empty line)
Expected output:
123
3.14159
True
False
[1, 2, 3]
(1, 2, 3)
{'name': 'Tom', 'age': 20}
Code analysis:
- When numbers are converted to strings, their original numeric representation is preserved.
- When Booleans are converted to strings, the first letter is uppercase.
- Container types such as lists, tuples and dictionaries are converted to their string representations.
Example 2: string concatenation
Example
name
=
"Tom"
age
=
20
height
=
1.75
Concatenate after converting with str()
info
=
name +
" "
str
(
age
)
" years old "
str
(
height
)
" m"
(
info
)
Output: Tom 20 years old 1.75 m
Use an f-string (recommended)
info
=
f
"{name} {age} years old {height} m"
(
info
)
Output: Tom 20 years old 1.75 m
Formatted output
(
"Name: "
str
(
name
)
)
Output: Name: Tom
Expected output:
Tom 20岁 1.75米
Tom 20岁 1.75米
姓名: Tom
Code analysis:
str()After conversion, strings can be concatenated with the plus sign.- Modern Python recommends f-strings for string formatting; they are more concise and readable.
Example 3: handling bytes and encodings
Example
Convert a byte string to a string (an encoding must be specified)
data
=
b
"Hello"
s
=
str
(
data
,
encoding
=
'utf-8'
)
(
s
)
Output: Hello
Handle encoding errors
data
=
b
"Hello\x80"
s
=
str
(
data
,
encoding
=
'utf-8'
,
errors
=
'ignore'
)
(
s
)
Output: Hello (invalid bytes ignored)
s
=
str
(
data
,
encoding
=
'utf-8'
,
errors
=
'replace'
)
(
s
)
Output: Hello (replaced with a replacement character)
Expected output:
Hello
Hello
Hello
This example shows str() its usage when dealing with bytes and encodings.
Python3 built-in function