'str' Literals and Conversions


How to specify "str" values in Python code?

✍: FYIcenter.com


"str" values can be specified using "str" literals or the str() conversion function as shown in this tutorial:

1. "str" literals in double quotes as shown below:

>>> "FYIcenter.com"
>>> "He says: \"No\""
'He says: "No"'
>>> "He says:\n\"No\""
'He says:\n"No"'

2. "str" literals in single quotes as shown below:

>>> 'FYIcenter.com'
>>> 'He says: "No"'
'He says: "No"'
>>> 'He says:\n"No"'
'He says:\n"No"'

3. "str" literals in triple quotes that allows you to express strings in multiple lines as shown below:

>>> """Spanning
... multiple
... lines"""
>>> '''Spanning
... multiple
... lines'''

4. "str" literals with "\" escape sequences as shown below:

>>> "Name\t\x41ge\n"
>>> "Name\t\u0041ge\n"

5. str() function converting other data types to "str":

>>> str(1234)
>>> str(3.14159)
>>> str(True)


Using Python Built-in Data Types

⇒⇒Python Tutorials

2017-09-09, 111👍, 0💬