String Basics
Strings are our first sequence-like data type that we get to manipulate in Python.
Last updated
Strings are our first sequence-like data type that we get to manipulate in Python.
Last updated
Strings are a data type in Python 3 that represent sequence of alphanumeric characters and special symbols. Strings will be enclosed with either 'single'
or "double"
quotations marks to denote them as strings. You cannot mix match the quotations.
Examples:
"a"
or 'a'
"1"
or '1'
"0010"
or '0010'
Variables can hold string type values: last_name = "Park"
A string value can be empty: string_variable = ''
A space is considered a non-empty string
Strings are comparable; therefore, you can create boolean expressions when comparing strings
'Hello' == 'hello'
evaluates to False because strings are also case-sensitive (also, the ascii value comparisons are different)
Whenever a string needs to be sorted or compared, it follows the ASCII order.
Numeric Symbols are less than uppercase characters
Uppercase characters are less than lowercase characters
Example:
Explanation: The first two characters of the string are equal; however, we now compare the third characters of 'k'
vs. 'n'
.
'n'
is considered greater than 'k'
; therefore, 'Jane'
is considered greater.
Determining the ASCII Value of a character
The ord()
function allow us to see a single character’s decimal value from the ASCII table. Our python interpreter will consider this value to help compare strings.
To do the reverse of ord()
, we can use the built-in chr()
function to determine the character from a ASCII unicode.
Immutability: The data type’s value cannot be altered without recreation of the value stored in a variable.
A single character or multiple characters cannot be changed in a string without redeclaring, recreating, or updating the variable with the intended change.
Integers, Floating Points, Booleans, and Strings are considered to be Immutable Data types.
Example: Examine the error
Source: Wikipedia