<h2>11.1 Introduction to Python Strings</h2>
<p>
A string is a sequence of characters enclosed inside quotation marks.
Strings are used to store text such as names, messages, addresses and
other textual information.
</p>
<pre><code>name = "Aman"
message = "Welcome to Python"
print(name)
print(message)</code></pre>
<p>Output:</p>
<pre><code>Aman
Welcome to Python</code></pre>
<h2>11.2 Creating Strings</h2>
<p>
Python allows strings to be created using single quotes, double quotes,
or triple quotes.
</p>
<pre><code>name1 = 'Aman'
name2 = "Ravi"
message = """Welcome
to Python
Programming"""
print(name1)
print(name2)
print(message)</code></pre>
<h2>11.3 Single and Double Quotes</h2>
<p>
Single and double quotation marks can both be used to create strings.
Choose the type that makes the text easier to write.
</p>
<pre><code>name = "Aman"
city = 'Gwalior'
print(name)
print(city)</code></pre>
<h2>11.4 Strings with Quotes</h2>
<p>
You can use one type of quotation mark inside another type.
</p>
<pre><code>message = "He said 'Hello'"
print(message)</code></pre>
<p>Output:</p>
<pre><code>He said 'Hello'</code></pre>
<h2>11.5 Multiline Strings</h2>
<p>
Triple quotes can be used to create strings that span multiple lines.
</p>
<pre><code>message = """Python is easy to learn.
Python is powerful.
Python is widely used."""
print(message)</code></pre>
<h2>11.6 String Length</h2>
<p>
The <code>len()</code> function returns the number of characters in
a string.
</p>
<pre><code>name = "Python"
print(len(name))</code></pre>
<p>Output:</p>
<pre><code>6</code></pre>
<h2>11.7 String Indexing</h2>
<p>
Each character in a string has a position called an index.
Python uses zero-based indexing, which means the first character has
index <code>0</code>.
</p>
<pre><code>word = "Python"
print(word[0])
print(word[1])
print(word[2])</code></pre>
<p>Output:</p>
<pre><code>P
y
t</code></pre>
<h2>11.8 Positive Indexing</h2>
<p>
Positive indexes start from the beginning of the string.
</p>
<pre><code>word = "Python"
print(word[0])
print(word[3])
print(word[5])</code></pre>
<p>Output:</p>
<pre><code>P
h
n</code></pre>
<h2>11.9 Negative Indexing</h2>
<p>
Negative indexes allow you to access characters starting from the end
of a string. The last character has index <code>-1</code>.
</p>
<pre><code>word = "Python"
print(word[-1])
print(word[-2])
print(word[-6])</code></pre>
<p>Output:</p>
<pre><code>n
o
P</code></pre>
<h2>11.10 String Slicing</h2>
<p>
String slicing is used to extract a portion of a string.
The basic syntax is:
</p>
<pre><code>string[start:end]</code></pre>
<p>
The start index is included, while the end index is not included.
</p>
<pre><code>word = "Python"
print(word[0:3])
print(word[2:5])</code></pre>
<p>Output:</p>
<pre><code>Pyt
tho</code></pre>
<h2>11.11 Slicing from the Beginning</h2>
<p>
The starting index can be omitted when you want to begin from the first
character.
</p>
<pre><code>word = "Python"
print(word[:4])</code></pre>
<p>Output:</p>
<pre><code>Pyth</code></pre>
<h2>11.12 Slicing to the End</h2>
<p>
The ending index can be omitted when you want to continue to the end
of the string.
</p>
<pre><code>word = "Python"
print(word[2:])</code></pre>
<p>Output:</p>
<pre><code>thon</code></pre>
<h2>11.13 Negative Slicing</h2>
<p>
Negative indexes can also be used while slicing.
</p>
<pre><code>word = "Python"
print(word[-4:])
print(word[:-2])</code></pre>
<p>Output:</p>
<pre><code>thon
Pyth</code></pre>
<h2>11.14 String Slicing with Step</h2>
<p>
A third value can be used to specify the step.
</p>
<pre><code>word = "Python"
print(word[0:6:2])</code></pre>
<p>Output:</p>
<pre><code>Pto</code></pre>
<h2>11.15 Reversing a String</h2>
<p>
A string can be reversed using slicing with a step of <code>-1</code>.
</p>
<pre><code>word = "Python"
reverse = word[::-1]
print(reverse)</code></pre>
<p>Output:</p>
<pre><code>nohtyP</code></pre>
<h2>11.16 Strings are Immutable</h2>
<p>
Strings in Python are immutable. This means that individual characters
of an existing string cannot be changed directly.
</p>
<pre><code>word = "Python"
# This is not allowed:
# word[0] = "J"</code></pre>
<p>
Instead, create a new string.
</p>
<pre><code>word = "Python"
word = "J" + word[1:]
print(word)</code></pre>
<p>Output:</p>
<pre><code>Jython</code></pre>
<h2>11.17 Joining Strings</h2>
<p>
The <code>+</code> operator can be used to join strings together.
This process is called concatenation.
</p>
<pre><code>first_name = "Aman"
last_name = "Gwal"
full_name = first_name + " " + last_name
print(full_name)</code></pre>
<p>Output:</p>
<pre><code>Aman Gwal</code></pre>
<h2>11.18 Repeating Strings</h2>
<p>
The multiplication operator can repeat a string multiple times.
</p>
<pre><code>word = "Hi "
print(word * 3)</code></pre>
<p>Output:</p>
<pre><code>Hi Hi Hi </code></pre>
<h2>11.19 Converting Values to Strings</h2>
<p>
The <code>str()</code> function converts a value into a string.
</p>
<pre><code>age = 20
message = "My age is " + str(age)
print(message)</code></pre>
<p>Output:</p>
<pre><code>My age is 20</code></pre>
<h2>11.20 Changing Letter Case</h2>
<p>
Python provides several methods for changing the case of characters
in a string.
</p>
<pre><code>text = "hello python"
print(text.upper())
print(text.lower())
print(text.title())
print(text.capitalize())</code></pre>
<p>Output:</p>
<pre><code>HELLO PYTHON
hello python
Hello Python
Hello python</code></pre>
<h2>11.21 upper() Method</h2>
<p>
The <code>upper()</code> method converts all letters to uppercase.
</p>
<pre><code>text = "python programming"
print(text.upper())</code></pre>
<p>Output:</p>
<pre><code>PYTHON PROGRAMMING</code></pre>
<h2>11.22 lower() Method</h2>
<p>
The <code>lower()</code> method converts all letters to lowercase.
</p>
<pre><code>text = "PYTHON PROGRAMMING"
print(text.lower())</code></pre>
<p>Output:</p>
<pre><code>python programming</code></pre>
<h2>11.23 title() Method</h2>
<p>
The <code>title()</code> method converts the first letter of each word
to uppercase.
</p>
<pre><code>text = "python programming language"
print(text.title())</code></pre>
<p>Output:</p>
<pre><code>Python Programming Language</code></pre>
<h2>11.24 capitalize() Method</h2>
<p>
The <code>capitalize()</code> method converts the first character of
the string to uppercase.
</p>
<pre><code>text = "python programming"
print(text.capitalize())</code></pre>
<p>Output:</p>
<pre><code>Python programming</code></pre>
<h2>11.25 strip() Method</h2>
<p>
The <code>strip()</code> method removes leading and trailing whitespace
from a string.
</p>
<pre><code>text = " Python "
print(text.strip())</code></pre>
<p>Output:</p>
<pre><code>Python</code></pre>
<h2>11.26 lstrip() and rstrip()</h2>
<pre><code>text = " Python "
print(text.lstrip())
print(text.rstrip())</code></pre>
<h2>11.27 replace() Method</h2>
<p>
The <code>replace()</code> method replaces one part of a string with
another.
</p>
<pre><code>text = "I like Java"
new_text = text.replace("Java", "Python")
print(new_text)</code></pre>
<p>Output:</p>
<pre><code>I like Python</code></pre>
<h2>11.28 split() Method</h2>
<p>
The <code>split()</code> method divides a string into a list.
By default, whitespace is used as the separator.
</p>
<pre><code>text = "Python is easy"
words = text.split()
print(words)</code></pre>
<p>Output:</p>
<pre><code>['Python', 'is', 'easy']</code></pre>
<h2>11.29 split() with a Separator</h2>
<pre><code>data = "apple,banana,mango"
fruits = data.split(",")
print(fruits)</code></pre>
<p>Output:</p>
<pre><code>['apple', 'banana', 'mango']</code></pre>
<h2>11.30 join() Method</h2>
<p>
The <code>join()</code> method combines elements of an iterable into
one string.
</p>
<pre><code>words = ["Python", "is", "easy"]
sentence = " ".join(words)
print(sentence)</code></pre>
<p>Output:</p>
<pre><code>Python is easy</code></pre>
<h2>11.31 Finding Text with find()</h2>
<p>
The <code>find()</code> method returns the position of the first
occurrence of a substring.
</p>
<pre><code>text = "Python Programming"
position = text.find("Program")
print(position)</code></pre>
<p>Output:</p>
<pre><code>7</code></pre>
<p>
If the requested text is not found, <code>find()</code> returns
<code>-1</code>.
</p>
<h2>11.32 Checking Text with in</h2>
<p>
The <code>in</code> operator can be used to check whether a substring
exists inside another string.
</p>
<pre><code>text = "Python Programming"
if "Python" in text:
print("Python is present")</code></pre>
<p>Output:</p>
<pre><code>Python is present</code></pre>
<h2>11.33 startswith() Method</h2>
<pre><code>text = "Python Programming"
print(text.startswith("Python"))
print(text.startswith("Java"))</code></pre>
<p>Output:</p>
<pre><code>True
False</code></pre>
<h2>11.34 endswith() Method</h2>
<pre><code>filename = "student.py"
print(filename.endswith(".py"))
print(filename.endswith(".html"))</code></pre>
<p>Output:</p>
<pre><code>True
False</code></pre>
<h2>11.35 count() Method</h2>
<p>
The <code>count()</code> method returns the number of times a substring
occurs in a string.
</p>
<pre><code>text = "banana"
print(text.count("a"))</code></pre>
<p>Output:</p>
<pre><code>3</code></pre>
<h2>11.36 Checking String Content</h2>
<p>
Python provides methods for checking whether a string contains letters,
numbers or whitespace.
</p>
<pre><code>text = "Python123"
print(text.isalpha())
print(text.isdigit())
print(text.isalnum())</code></pre>
<p>Output:</p>
<pre><code>False
False
True</code></pre>
<h2>11.37 isalpha()</h2>
<p>
The <code>isalpha()</code> method returns <code>True</code> when all
characters in the string are alphabetic.
</p>
<pre><code>print("Python".isalpha())
print("Python123".isalpha())</code></pre>
<p>Output:</p>
<pre><code>True
False</code></pre>
<h2>11.38 isdigit()</h2>
<p>
The <code>isdigit()</code> method checks whether all characters are
digits.
</p>
<pre><code>print("12345".isdigit())
print("123a".isdigit())</code></pre>
<p>Output:</p>
<pre><code>True
False</code></pre>
<h2>11.39 f-Strings</h2>
<p>
F-strings provide a convenient way to insert variables and expressions
inside strings.
</p>
<pre><code>name = "Aman"
age = 20
message = f"My name is {name} and I am {age} years old."
print(message)</code></pre>
<p>Output:</p>
<pre><code>My name is Aman and I am 20 years old.</code></pre>
<h2>11.40 Formatting Expressions with f-Strings</h2>
<p>
Expressions can also be placed inside curly braces in an f-string.
</p>
<pre><code>a = 10
b = 20
print(f"The sum is {a + b}")</code></pre>
<p>Output:</p>
<pre><code>The sum is 30</code></pre>
<h2>11.41 String Formatting with format()</h2>
<p>
The <code>format()</code> method can also be used to insert values
into a string.
</p>
<pre><code>name = "Aman"
age = 20
message = "My name is {} and I am {} years old."
print(message.format(name, age))</code></pre>
<h2>11.42 Escape Characters</h2>
<p>
Escape characters are used to represent special characters inside
strings.
</p>
<table>
<thead>
<tr>
<th>Escape Character</th>
<th>Meaning</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>\n</code></td>
<td>New line</td>
</tr>
<tr>
<td><code>\t</code></td>
<td>Tab</td>
</tr>
<tr>
<td><code>\\</code></td>
<td>Backslash</td>
</tr>
<tr>
<td><code>\"</code></td>
<td>Double quotation mark</td>
</tr>
<tr>
<td><code>\'</code></td>
<td>Single quotation mark</td>
</tr>
</tbody>
</table>
<h2>11.43 New Line Example</h2>
<pre><code>message = "Hello\nWelcome to Python"
print(message)</code></pre>
<p>Output:</p>
<pre><code>Hello
Welcome to Python</code></pre>
<h2>11.44 Tab Example</h2>
<pre><code>print("Name:\tAman")
print("Age:\t20")</code></pre>
<p>Output:</p>
<pre><code>Name: Aman
Age: 20</code></pre>
<h2>11.45 Raw Strings</h2>
<p>
A raw string treats backslashes as ordinary characters. Raw strings
are commonly useful when working with file paths and regular
expressions.
</p>
<pre><code>path = r"C:\Users\Aman\Documents"
print(path)</code></pre>
<h2>11.46 Comparing Strings</h2>
<p>
Strings can be compared using comparison operators.
Python compares strings based on their character values.
</p>
<pre><code>a = "apple"
b = "banana"
print(a == b)
print(a != b)</code></pre>
<p>Output:</p>
<pre><code>False
True</code></pre>
<h2>11.47 Looping Through a String</h2>
<p>
A <code>for</code> loop can be used to process each character of
a string.
</p>
<pre><code>word = "Python"
for character in word:
print(character)</code></pre>
<p>Output:</p>
<pre><code>P
y
t
h
o
n</code></pre>
<h2>11.48 Counting Characters</h2>
<pre><code>text = "programming"
count = 0
for character in text:
if character == "m":
count += 1
print("m appears", count, "times")</code></pre>
<p>Output:</p>
<pre><code>m appears 2 times</code></pre>
<h2>11.49 Example: Username Validation</h2>
<pre><code>username = input("Enter username: ")
if len(username) >= 5:
print("Username length is valid")
else:
print("Username must contain at least 5 characters")</code></pre>
<h2>11.50 Example: Simple Email Check</h2>
<pre><code>email = input("Enter your email: ")
if "@" in email and "." in email:
print("Email format looks valid")
else:
print("Please enter a valid email address")</code></pre>
<h2>11.51 Example: Count Vowels</h2>
<pre><code>text = input("Enter a word: ")
vowels = "aeiou"
count = 0
for character in text.lower():
if character in vowels:
count += 1
print("Number of vowels:", count)</code></pre>
<h2>11.52 Example: Reverse a String</h2>
<pre><code>text = input("Enter text: ")
reverse = text[::-1]
print("Reversed:", reverse)</code></pre>
<h2>11.53 Example: Palindrome Checker</h2>
<p>
A palindrome is a word or sequence that reads the same forward and
backward.
</p>
<pre><code>text = input("Enter text: ")
if text == text[::-1]:
print("Palindrome")
else:
print("Not a palindrome")</code></pre>
<h2>11.54 Example: Count Words</h2>
<pre><code>sentence = input("Enter a sentence: ")
words = sentence.split()
print("Number of words:", len(words))</code></pre>
<h2>11.55 Common String Methods</h2>
<table>
<thead>
<tr>
<th>Method</th>
<th>Purpose</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>upper()</code></td>
<td>Converts text to uppercase</td>
</tr>
<tr>
<td><code>lower()</code></td>
<td>Converts text to lowercase</td>
</tr>
<tr>
<td><code>title()</code></td>
<td>Capitalizes each word</td>
</tr>
<tr>
<td><code>strip()</code></td>
<td>Removes surrounding whitespace</td>
</tr>
<tr>
<td><code>replace()</code></td>
<td>Replaces text</td>
</tr>
<tr>
<td><code>split()</code></td>
<td>Splits a string into a list</td>
</tr>
<tr>
<td><code>join()</code></td>
<td>Combines elements into a string</td>
</tr>
<tr>
<td><code>find()</code></td>
<td>Finds the position of text</td>
</tr>
<tr>
<td><code>count()</code></td>
<td>Counts occurrences</td>
</tr>
<tr>
<td><code>startswith()</code></td>
<td>Checks the beginning of a string</td>
</tr>
<tr>
<td><code>endswith()</code></td>
<td>Checks the end of a string</td>
</tr>
</tbody>
</table>
<h2>11.56 Chapter Summary</h2>
<p>
In this chapter, you learned how to work with Python strings and
perform common string operations.
</p>
<ul>
<li>Creating strings</li>
<li>Single, double and triple quotes</li>
<li>Finding string length</li>
<li>Positive and negative indexing</li>
<li>String slicing</li>
<li>Reversing strings</li>
<li>String concatenation</li>
<li>String repetition</li>
<li>Changing letter case</li>
<li>Removing whitespace</li>
<li>Replacing text</li>
<li>Splitting and joining strings</li>
<li>Searching within strings</li>
<li>Checking string content</li>
<li>F-strings and string formatting</li>
<li>Escape characters</li>
<li>Practical string programs</li>
</ul>
<h2>11.57 Quick Revision Questions</h2>
<ol>
<li>What is a string in Python?</li>
<li>What is the first index of a string?</li>
<li>What is negative indexing?</li>
<li>How do you find the length of a string?</li>
<li>What is string slicing?</li>
<li>Are Python strings mutable?</li>
<li>What is the difference between <code>upper()</code> and <code>lower()</code>?</li>
<li>What does <code>split()</code> do?</li>
<li>What does <code>join()</code> do?</li>
<li>What are f-strings?</li>
</ol>
<h2>11.58 Practice Exercises</h2>
<ol>
<li>Create a string containing your full name and print it.</li>
<li>Print the first and last character of a string.</li>
<li>Reverse a string using slicing.</li>
<li>Count the number of vowels in a sentence.</li>
<li>Count the number of words in a sentence.</li>
<li>Convert a sentence to uppercase and lowercase.</li>
<li>Replace one word with another word.</li>
<li>Check whether a string is a palindrome.</li>
<li>Create a program to count a particular character.</li>
<li>Create a simple username validation program.</li>
</ol>
<h2>11.59 Mini Project: Text Analyzer</h2>
<p>
Create a program that accepts a sentence from the user and displays
useful information about it.
</p>
<pre><code>text = input("Enter a sentence: ")
print("Original:", text)
print("Length:", len(text))
print("Uppercase:", text.upper())
print("Lowercase:", text.lower())
print("Words:", len(text.split()))
print("Reversed:", text[::-1])</code></pre>
<h2>What's Next?</h2>
<p>
<strong>
Next Chapter: Python Lists – Creating, Accessing, Modifying and
Working with List Methods
</strong>
</p>