Day 11 – Strings in Python

🚀 Passionate DevOps Engineer with expertise in cloud computing, CI/CD, and automation. Skilled in Linux, Docker, Kubernetes, Terraform, Ansible, and Jenkins. I specialize in building scalable, secure, and automated infrastructures, optimizing software delivery pipelines, and integrating DevSecOps practices. Always exploring new ways to enhance deployment workflows and bridge the gap between development and operations.
Welcome to Day 11 of 100 Days of Python!
Almost every useful program works with text in some way.
Names, messages, usernames, sentences, file contents, commands, and even data received from users are commonly represented as strings in Python.
Today, we will learn:
What strings are
How to create strings
Single and double quotes
Using quotation marks inside strings
Multiline strings
Accessing individual characters using indexes
String indexing and the
IndexErrorLooping through a string using a
forloop
1. What is a String?
A string is a sequence of characters used to represent text.
In Python, strings are created by enclosing text inside quotation marks.
We can use:
Single quotes
' 'Double quotes
" "Triple single quotes
''' '''Triple double quotes
""" """
For example:
name = "Sritesh"
Here, "Sritesh" is a string.
We can also write:
name = 'Sritesh'
Both represent the same text.
2. Creating a String
Let's create a simple string:
name = "Sritesh"
print(name)
Output:
Sritesh
We can also combine strings with other strings using the + operator.
name = "Sritesh"
print("Hello, " + name + "!")
Output:
Hello, Sritesh!
Here, the + operator joins the strings together. This is called string concatenation.
3. Single Quotes vs Double Quotes
Python allows us to use either single or double quotation marks.
For example:
name1 = "Sritesh"
name2 = 'Sritesh'
print(name1)
print(name2)
Both produce:
Sritesh
Sritesh
So, in many situations, the choice between single and double quotes is simply a matter of style or convenience.
The important thing is to use matching quotation marks.
4. Using Quotes Inside a String
Sometimes a sentence itself contains quotation marks.
For example:
He said, "I want to eat an apple".
If we try to write it using double quotes like this:
print("He said, "I want to eat an apple".")
Python cannot correctly determine where the string starts and ends.
One simple solution is to use single quotes around the entire string:
print('He said, "I want to eat an apple".')
Output:
He said, "I want to eat an apple".
This is one reason Python supporting both single and double quotes is convenient.
5. Multiline Strings
Sometimes we need to store text that spans multiple lines.
Python allows us to create multiline strings using triple quotes.
For example:
message = """He said,
Hi Sritesh!
Hey! I am Good.
"I want to eat an apple."
"""
print(message)
Output:
He said,
Hi Sritesh!
Hey! I am Good.
"I want to eat an apple."
We can use either:
'''
multiple
lines
'''
or:
"""
multiple
lines
"""
for multiline string literals.
6. Strings Are Sequences of Characters
A string is not treated as one indivisible piece of text.
Python stores it as a sequence of characters.
For example:
name = "Sritesh"
The string contains these characters:
S r i t e s h
Each character has a position called an index.
7. String Indexing
Python uses zero-based indexing.
This means the first character has index 0, the second has index 1, and so on.
For:
name = "Sritesh"
the indexes are:
Character: S r i t e s h
Index: 0 1 2 3 4 5 6
We can access a character using square brackets:
print(name[0])
Output:
S
Similarly:
print(name[1])
Output:
r
And:
print(name[2])
Output:
i
8. Accessing Characters Individually
Let's access every character in "Sritesh":
name = "Sritesh"
print(name[0])
print(name[1])
print(name[2])
print(name[3])
print(name[4])
print(name[5])
print(name[6])
Output:
S
r
i
t
e
s
h
The important rule is:
String indexes start from
0.
So if a string contains 7 characters, the indexes are:
0 1 2 3 4 5 6
The last index is therefore length - 1.
9. What Happens If the Index Does Not Exist?
Suppose we have:
name = "Sritesh"
There are only 7 characters, so the valid indexes are:
0 to 6
If we try:
print(name[7])
Python raises an error because index 7 does not exist.
The error is:
IndexError: string index out of range
For example:
name = "Sritesh"
print(name[7])
This will result in an IndexError.
So always make sure the index exists before trying to access a character.
10. Looping Through a String
Because a string is a sequence of characters, we can use a for loop to process each character one by one.
Example:
name = "Sritesh"
for character in name:
print(character)
Output:
S
r
i
t
e
s
h
The loop automatically goes through each character in the string.
The basic structure is:
for character in string:
print(character)
Here, character is a loop variable that receives one character at a time.
11. Looping Through a Multiline String
We can also loop through a multiline string.
For example:
message = """Hi Sritesh!
Hey! I am Good.
I am learning Python."""
for character in message:
print(character)
The loop processes every character, including spaces and newline characters.
This is why the output may appear line by line when the original string contains multiple lines.
12. Complete Program
Here is the program from today's practice:
name = "Sritesh"
friend = "Jyoti"
anotherFriend = "Rohit"
apple = '''He said,
Hi Sritesh!
Hey! I am Good.
"I want to eat an apple." '''
print("Hello, " + name + "!")
print(apple)
print(name[0])
print(name[1])
print(name[2])
print(name[3])
print(name[4])
print(name[5])
print(name[6])
print("Let's use a for loop\n")
for character in apple:
print(character)
13. Understanding the Program
Let's break the program into smaller parts.
Creating strings
name = "Sritesh"
friend = "Jyoti"
anotherFriend = "Rohit"
Three string variables are created.
Creating a multiline string
apple = '''He said,
Hi Sritesh!
Hey! I am Good.
"I want to eat an apple." '''
Triple single quotes allow the text to span multiple lines.
Concatenating strings
print("Hello, " + name + "!")
This joins:
"Hello, "
with:
"Sritesh"
and:
"!"
Result:
Hello, Sritesh!
Accessing characters
print(name[0])
Accesses the first character.
print(name[6])
Accesses the last character of "Sritesh".
Looping through characters
for character in apple:
print(character)
This processes the multiline string character by character.
14. Important String Concepts
Here are the main concepts from today's lesson:
| Concept | Meaning |
|---|---|
| String | A sequence of characters representing text |
'text' |
String using single quotes |
"text" |
String using double quotes |
'''text''' |
Triple-quoted string |
"""text""" |
Triple-quoted string |
+ |
Concatenates strings |
string[index] |
Accesses a character |
Index 0 |
First character |
for character in string |
Iterates through characters |
IndexError |
Raised when a string index is out of range |
15. Quick Revision
Creating a string
name = "Sritesh"
Single quotes
name = 'Sritesh'
Concatenation
print("Hello, " + name)
Multiline string
message = """Line 1
Line 2
Line 3"""
Accessing a character
print(name[0])
Looping through a string
for character in name:
print(character)
Remember zero-based indexing
For:
name = "Sritesh"
S → 0
r → 1
i → 2
t → 3
e → 4
s → 5
h → 6
16. Key Takeaways
After completing Day 11, you should understand:
What a string is in Python
How to create strings using different quotation marks
The difference between single and double quotes in terms of convenience
How to include quotation marks inside strings
How to create multiline strings
That strings are sequences of characters
How zero-based indexing works
How to access individual characters
Why an invalid index produces an
IndexErrorHow to loop through a string using a
forloopHow to concatenate strings using
+
Strings are one of the most frequently used data types in Python. We will build on these fundamentals later when we learn string methods, slicing, formatting, and more advanced text processing.
📂 Day 10 Resources
All notes and code for this day are available in the GitHub repository:
https://github.com/SriteshSuranjan/100-Days-of-Python/tree/main/11-Day11-Strings




