Strings#
Strings, in Python, are a fundamental data type used to represent text. Whether it’s a single character or a large paragraph, any text enclosed within either single quotes (‘’) or double quotes (“”) is considered a string in Python. These strings serve as the building blocks for text manipulation, and Python equips us with a rich set of built-in methods, or manipulators, to operate on strings. You can perform operations like changing letter cases, slicing, and much more to manipulate and extract valuable information from text data.
In Python, declaring variables is straightforward. You simply assign a value to a variable name using the assignment operator (=). For example, you can declare variables like this:
# Declaring basic strings
name = "Brad"
location = 'Halifax'
occupation = 'DevOps Engineer'
Note
Strings, as mentioned, can be enclosed in either single quotes (''
) or double quotes (""
), allowing you flexibility in your code. Moreover, if a string needs to contain both types of quotes, you can use the backslash (\) as an escape character to ensure Python interprets the characters correctly.
String Methods#
Strings can accept methods which alter the contents of the string. Some examples of string methods that are built into Python include:
book_title = "mastery by Robert greene"
print(book_title.upper())
print(book_title.lower())
print(book_title.title())
print(book_title.capitalize())
MASTERY BY ROBERT GREENE
mastery by robert greene
Mastery By Robert Greene
Mastery by robert greene
Concatenating Strings#
Strings can be combined together with simple plus (+
) signs:
first_name = "Albert"
last_name = "Einstien"
print("There was a famous scientist named " + first_name + " " + last_name + ".")
There was a famous scientist named Albert Einstien.
F-Strings#
Introduced in version 3.6 (3.11.5 is current as of Fall 2023), “F-Strings” are a much improved way to build strings in Python. Simply preceed a string with f
before the opening quotation. Then any variable can be surrouned by curly braces {}
, removing the need to cast variables of various types. For example:
name = "Jim"
age = 32
print(f"My name is {name} and I am {age} years old!")
My name is Jim and I am 32 years old!
Adding & Removing Whitespace#
Special characters can be added to strings that add whitespace. These include:
\t
which adds a tab (4 spaces)\n
which adds a newline
Example:
print("Hello, I'm \t \t Brad!")
print("and you are learning \n \t Python")
Hello, I'm Brad!
and you are learning
Python
Whitespace can also be stripped out using methods. These include strip()
(both sides), rstrip()
(right side) and lstrip
(left side).
Example:
name = " Albert Einstein "
print("Hello I am " + name + ".") # whitespace remains
print("Hello I am " + name.strip() + ".") # whitespace stripped
print("Hello I am " + name.rstrip() + ".") # whitespace on righ stripped
print("Hello I am " + name.lstrip() + ".") # whitespace on left stripped
Hello I am Albert Einstein .
Hello I am Albert Einstein.
Hello I am Albert Einstein.
Hello I am Albert Einstein .