A string in Python is a sequence of characters used to represent text.
Strings can be written using either single quotes (' ') or double quotes (" ")—both work exactly the same way.
Displaying a String
You can show text on the screen using the
print()
function.
Using Quotes Inside a String
You can include quotation marks inside a string as long as they are
different from the quotes used to define the string.
Assigning a String to a Variable
Strings are commonly stored in variables so they can be reused later in a
program.
Multi-Line Strings
Python allows you to store text across multiple lines using triple quotes
(""" """ or
''' ''').
Line breaks in the code are preserved in the output.
Example (Triple Double Quotes)
Example (Triple Single Quotes)
Strings as Sequences
In Python, strings behave like sequences (arrays) of Unicode characters.
Each character has a position index, starting from 0.
This prints the character at index position 2.
Looping Through a String
Since strings are iterable, you can loop through each character using a
for loop.
Finding the Length of a String
Use the
len()
function to determine how many characters a string contains.
Checking for Text Inside a String
You can check whether a word or character exists in a string using the
in keyword.
Using 'in' with an if Statement
Checking if Text Is NOT Present
To verify that a word or character does not exist in a string, use the
not in
keyword.
Using 'not in' with an if Statement
Python String Slicing
String slicing allows you to extract a specific portion of a string by
selecting a range of characters. This is done using square brackets
[] with a
start index and an end index, separated by a colon.
The start index is inclusive, and the end index is exclusive.
String indexing always begins at 0.
Basic Slice Syntax
Slicing From the Beginning
If you omit the starting index, Python automatically begins slicing from the
first character.
Slicing Until the End
If you leave out the ending index, Python slices the string all the way to
the last character.
Using Negative Indexes
Negative indexing allows you to count characters from the end of the string
(-1 refers to the last character).
Modifying Strings in Python
Python provides many built-in methods that allow you to change or manipulate
string values easily. These methods do not modify the original
string—instead, they return a new modified string.
Converting Text to Uppercase
Converting Text to Lowercase
Removing Extra Spaces
Replacing Part of a String
Splitting a String into a List
Combining Strings in Python
In Python, string concatenation means joining two or more strings into a
single string. This is commonly done using the
+ operator.
Joining Two Strings
Adding Space Between Strings
Concatenating Multiple Strings
Python – String Formatting
In Python, strings and numbers cannot be directly joined using the
+ operator.
Attempting to do so will raise a TypeError because Python treats text and
numeric values as different data types.
Incorrect Example (Raises Error)
To safely combine text with numbers, Python provides string formatting
techniques such as f-strings.
Basic F-String Example
Using Variables Inside Placeholders
Formatting Floating-Point Numbers
Using Expressions Inside F-Strings
Python – Escape Characters
Escape characters are special sequences used in Python strings to represent
characters that cannot be typed directly or would otherwise break the string
syntax.