# Day 11 – Strings in Python

![](https://cdn.hashnode.com/uploads/covers/664f77938fc1f806b829b90b/534bc0e4-3a6d-4784-84e5-254f0622e745.jpg align="center")

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 `IndexError`
    
*   Looping through a string using a `for` loop
    

* * *

# 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:

```python
name = "Sritesh"
```

Here, `"Sritesh"` is a string.

We can also write:

```python
name = 'Sritesh'
```

Both represent the same text.

* * *

# 2\. Creating a String

Let's create a simple string:

```python
name = "Sritesh"

print(name)
```

Output:

```text
Sritesh
```

We can also combine strings with other strings using the `+` operator.

```python
name = "Sritesh"

print("Hello, " + name + "!")
```

Output:

```text
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:

```python
name1 = "Sritesh"
name2 = 'Sritesh'

print(name1)
print(name2)
```

Both produce:

```text
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:

```text
He said, "I want to eat an apple".
```

If we try to write it using double quotes like this:

```python
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:

```python
print('He said, "I want to eat an apple".')
```

Output:

```text
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:

```python
message = """He said,
Hi Sritesh!
Hey! I am Good.
"I want to eat an apple."
"""

print(message)
```

Output:

```text
He said,
Hi Sritesh!
Hey! I am Good.
"I want to eat an apple."
```

We can use either:

```python
'''
multiple
lines
'''
```

or:

```python
"""
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:

```python
name = "Sritesh"
```

The string contains these characters:

```text
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:

```python
name = "Sritesh"
```

the indexes are:

```text
Character:  S   r   i   t   e   s   h
Index:      0   1   2   3   4   5   6
```

We can access a character using square brackets:

```python
print(name[0])
```

Output:

```text
S
```

Similarly:

```python
print(name[1])
```

Output:

```text
r
```

And:

```python
print(name[2])
```

Output:

```text
i
```

* * *

# 8\. Accessing Characters Individually

Let's access every character in `"Sritesh"`:

```python
name = "Sritesh"

print(name[0])
print(name[1])
print(name[2])
print(name[3])
print(name[4])
print(name[5])
print(name[6])
```

Output:

```text
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:

```text
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:

```python
name = "Sritesh"
```

There are only 7 characters, so the valid indexes are:

```text
0 to 6
```

If we try:

```python
print(name[7])
```

Python raises an error because index `7` does not exist.

The error is:

```text
IndexError: string index out of range
```

For example:

```python
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:

```python
name = "Sritesh"

for character in name:
    print(character)
```

Output:

```text
S
r
i
t
e
s
h
```

The loop automatically goes through each character in the string.

The basic structure is:

```python
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:

```python
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:

```python
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

```python
name = "Sritesh"
friend = "Jyoti"
anotherFriend = "Rohit"
```

Three string variables are created.

### Creating a multiline string

```python
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

```python
print("Hello, " + name + "!")
```

This joins:

```text
"Hello, "
```

with:

```text
"Sritesh"
```

and:

```text
"!"
```

Result:

```text
Hello, Sritesh!
```

### Accessing characters

```python
print(name[0])
```

Accesses the first character.

```python
print(name[6])
```

Accesses the last character of `"Sritesh"`.

### Looping through characters

```python
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

```python
name = "Sritesh"
```

### Single quotes

```python
name = 'Sritesh'
```

### Concatenation

```python
print("Hello, " + name)
```

### Multiline string

```python
message = """Line 1
Line 2
Line 3"""
```

### Accessing a character

```python
print(name[0])
```

### Looping through a string

```python
for character in name:
    print(character)
```

### Remember zero-based indexing

For:

```python
name = "Sritesh"
```

```text
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 `IndexError`
    
*   How to loop through a string using a `for` loop
    
*   How 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] 

* * *
