Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python str() Function

Python str() Function

Python3 Built-in Functions Python3 built-in 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

- Type: any object - Description: the object to convert into a string. - Type: string - Description: the encoding format (such as 'utf-8' or 'gbk'). - Type: string - Description: the error handling scheme, defaulting to 'strict' (raise an exception). Other options: 'ignore', 'replace'.

Function description


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

print

(

str

(

123

)

)

         

Output: 123

print

(

type

(

str

(

123

)

)

)

   

Output:

Convert a float to a string

print

(

str

(

3.14159

)

)

     

Output: 3.14159

Convert a Boolean to a string

print

(

str

(

True

)

)

         

Output: True

print

(

str

(

False

)

)

       

Output: False

Convert a list to a string

print

(

str

(

[

1

,

2

,

3

]

)

)

   

Output: [1, 2, 3]

Convert a tuple to a string

print

(

str

(

(

1

,

2

,

3

)

)

)

   

Output: (1, 2, 3)

Convert a dictionary to a string

print

(

str

(

{

"name"

:

"Tom"

,

"age"

:

20

}

)

)

 

Output: {'name': 'Tom', 'age': 20}

No arguments

print

(

str

(

)

)

             

Output: (empty line)

Expected output:

123

3.14159
True
False
[1, 2, 3]
(1, 2, 3)
{'name': 'Tom', 'age': 20}

Code analysis:

  1. When numbers are converted to strings, their original numeric representation is preserved.
  2. When Booleans are converted to strings, the first letter is uppercase.
  3. 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"

print

(

info

)

 

Output: Tom 20 years old 1.75 m

Use an f-string (recommended)

info

=

f

"{name} {age} years old {height} m"

print

(

info

)

 

Output: Tom 20 years old 1.75 m

Formatted output

print

(

"Name: "

str

(

name

)

)

 

Output: Name: Tom

Expected output:

Tom 20岁 1.75米
Tom 20岁 1.75米
姓名: Tom

Code analysis:

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'

)

print

(

s

)

 

Output: Hello

Handle encoding errors

data

=

b

"Hello\x80"

s

=

str

(

data

,

encoding

=

'utf-8'

,

errors

=

'ignore'

)

print

(

s

)

 

Output: Hello (invalid bytes ignored)

s

=

str

(

data

,

encoding

=

'utf-8'

,

errors

=

'replace'

)

print

(

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 Functions Python3 built-in function