Python Tutorials - Herong's Tutorial Examples - v2.14, by Herong Yang
Data Type - 'str' for Character String
This section describes the 'str' data type, which stores a sequence of Unicode characters.
What Is the "str" Data Type? "str" is the string data type in Python. Each "str" object stores a string, or sequence, of Unicode characters.
"str" data type has the following main Features.
1. "str" data objects can be created in several ways:
Here are some examples on how to create "str" objects:
>>> 'Say "Hi!"' 'Say "Hi!"' >>> "Newton's laws" "Newton's laws" # any special or non-ASCII character can be escaped >>> 'Say\x0aHi!' 'Say\nHi!' >>> str(b'Say "Hi!"', 'utf-8') 'Say "Hi!"'
2. "str" data type provides an array-style expression to access the character of a given position.
>>> x = 'apple' >>> type(x) <class 'str'> >>> c = x[0] >>> c 'a' >>> type(c) <class 'str'>
3. "str" data type is an immutable data type. Once a "str" data object is created to store a character sequence, this sequence will never change.
>>> x = 'apple' >>> c = x[0] >>> c 'a' >>> x[0] = 'b' TypeError: 'str' object does not support item assignment
Don't get confused about "str" immutability and "str" variable re-assignment capability. A "str" variable can be re-assigned with different "bytes" objects many times.
# new object is created for 'apple' and assigned to x >>> x = 'apple' >>> id(x) 4459205168 # assign the same object to y >>> y = x >>> id(y) 4459205168 # new object is created for 'orange' and assigned to x >>> x = 'orange' >>> id(x) 4459205488 # the first object for 'apple' is still there in memory >>> y 'apple' >>> id(y) 4459205168
4. Because "str" data type is immutable, an object created for a given character sequence is not allowed to change its value. This allow "str" objects to be cached in memory and reused later whenever they are needed again. Reusing objects will reduce execution time and memory consumption.
On my macOS computer, "str" objects are cached and reused in some cases, not in all cases. The code example below shows you that there are 2 "str" objects created for 4 requests of 'apple'.
>>> x = 'apple' >>> y = 'apple' >>> (id(x), id(y), id('apple'), id(str(b"apple", 'ascii'))) (4459205168, 4459205168, 4459205168, 4459205680)
5. "str" objects support the following operations:
Syntax Operation Note ---------- ------------- ------------------ x + y Concatenation x * n Repetition n is an "int" n * x Repetition Same as x * n s in x Look up s is a sub "str" s not in x Look up Same as !(s in x)
6. "str" objects support the following comparison operations. They are performed by compare the character value (Unicode point) from each position starting from the left side.
Syntax Operation ------ --------- x < y Less than x <= y Less than or equal x > y Greater than x >= y Greater than or equal x == y Equal x != y Not equal
7. Some built-in functions are provided for "str" objects.
8. Some instance methods are provided for "str" objects.
Table of Contents
Common Features of All Data Types
Data Type - NoneType for Nothing
Data Type - 'bool' for Boolean Values
Data Type - 'int' for Integer Values
Data Type - 'float' for Real Numbers
Data Type - 'bytes' for Byte Sequence
►Data Type - 'str' for Character String
Data Type - 'tuple' for Immutable List
Data Type - 'list' for Mutable List
Data Type - 'dict' for Dictionary Table
Variables, Operations and Expressions
Function Statement and Function Call
Iterators, Generators and List Comprehensions
Packages and Package Directories
"pathlib" - Object-Oriented Filesystem Paths
"pip" - Package Installer for Python
SciPy.org - Python Libraries for Science
pandas - Data Analysis and Manipulation
Anaconda - Python Environment Manager