Python Strings
1. What is a String?
A string is text.
In Python, any text written inside:
-
" "(double quotes) or -
' '(single quotes)
is called a string.
Example
name = "Python"
city = 'Delhi'
2. Printing a String
Example
print("Hello World")
Print a String Variable
language = "Python"
print(language)
3. String Length
You can find how many characters are in a string using len().
Example
word = "Python"
print(len(word))
Output:
6
4. Accessing Characters (Indexing)
Each character in a string has a position (index).
Index starts from 0.
Example
text = "Python"
print(text[0]) # P
print(text[1]) # y
print(text[-1]) # n
5. Slicing Strings
Slicing means taking a part of the string.
Example
text = "Programming"
print(text[0:4]) # Prog
print(text[4:]) # ramming
print(text[:6]) # Progra
6. Joining Strings (Concatenation)
You can join strings using +.
Example
first = "Hello"
second = "World"
print(first + " " + second)
7. String with Numbers
Strings and numbers cannot be joined directly.
❌ Wrong Example
age = 25
print("Age is " + age)
✔ Correct Example
print("Age is " + str(age))
OR (easy way)
print("Age is", age)
8. Common String Methods
Python has many built-in string functions.
Uppercase
text = "python"
print(text.upper())
Lowercase
print(text.lower())
Capitalize First Letter
print(text.capitalize())
Replace Text
text = "I like Java"
print(text.replace("Java", "Python"))
9. Checking Text in a String
Example
text = "Python is easy"
print("Python" in text)
print("Java" in text)
10. Taking String Input from User
Example
name = input("Enter your name: ")
print("Hello", name)
11. Multiline Strings
You can write text in many lines using triple quotes.
Example
message = """Welcome to Python
This is a simple string example
Enjoy learning"""
print(message)
12. Common Beginner Mistakes
❌ Missing Quotes
name = Python
✔ Correct:
name = "Python"
❌ Wrong Index
text = "Hi"
print(text[5])
❌ Error: index out of range
13. Simple Practice Examples
Example 1: Print Full Name
first_name = "Amit"
last_name = "Sharma"
print(first_name + " " + last_name)
Example 2: Count Letters
word = "Apple"
print(len(word))
Example 3: Change Case
text = "python programming"
print(text.upper())
14. Summary (Python Strings)
✔ Strings are text
✔ Written inside quotes
✔ len() gives length
✔ Index starts from 0
✔ Many useful string methods
📘 Perfect for Beginner eBook
This chapter is ideal for:
-
Python beginner books
-
School & college students
-
Self-learners
If you want next, I can write:
-
String Exercises
-
Python Booleans
-
Operators
-
if–else using strings
-
MCQs with answers
Just tell me 😊
