Estimated reading time: 3.1 mins read
What is a String in Python?
A string in Python is a sequence of characters enclosed within quotes.
Python supports three types of quotes to create strings:
- Single quotes (
' ') - Double quotes (
" ") - Triple quotes (
''' '''or""" """)
🔹 Important: Strings in Python are immutable, meaning once created, they cannot be changed.
Creating Strings in Python
str1 = "Learning Python from scratch"
str2 = "makes programming easy"
str3 = '''This is a multi-line string example'''
Explanation:
str1andstr2use double quotesstr3uses triple quotes, commonly used for multi-line strings- All three variables store string values
String Concatenation (Joining Strings)
String concatenation means joining two or more strings using the + operator.
print("Str1 + Str2 is:", str1 + " " + str2)
Output:
Str1 + Str2 is: Learning Python from scratch makes programming easy
Finding the Length of a String
The len() function returns the total number of characters, including spaces.
print("Length of str1 is:", len(str1))
print("Length of str2 is:", len(str2))
String Indexing
Each character in a string has an index number, starting from 0.
str4 = "python basics"
print("Character at index 5 is:", str4[5])
Output:
Character at index 5 is: n
String Slicing
Slicing allows you to extract a portion of a string.
Syntax:
string[start : end]
The end index is excluded
str5 = "Python Programming"
print("Slice 'Python':", str5[0:6])
print("Slice 'Programming':", str5[7:len(str5)])
Skipping Start or End Index
print("Complete string using slicing:", str5[:])
print("Only 'Programming':", str5[7:])
Negative Indexing
Negative indexing starts from the end of the string.
-1→ last character-2→ second last character
str6 = "Data Science"
print("Extract 'Science':", str6[-7:])
print("Extract 'Data':", str6[:-8])
Common String Methods
Python provides powerful built-in string methods.
str7 = "python is very easy to learn"
endswith()
Checks if the string ends with a specific value.
print("Ends with 'learn':", str7.endswith("learn"))
capitalize()
Capitalizes the first character of the string.
print("Capitalized string:", str7.capitalize())
🔹 Strings are immutable, so the original string does not change unless reassigned.
print("Original string:", str7)
str7 = str7.capitalize()
print("Updated string:", str7)
replace() Method
Used to replace characters or words in a string.
str8 = "Orange"
print("Original str8:", str8)
print("Replace 'e' with 'a':", str8.replace("e", "a"))
str9 = "This is my first Laptop"
print("Original str9:", str9)
print("After replacement:", str9.replace("first", "new"))
find() Method
Returns the index of the first occurrence of a substring.
Returns -1 if the substring is not found.
print("Index of 'i' in str9:", str9.find("i"))
print("Index of 'Laptop':", str9.find("Laptop"))
print("Find 'z' (not present):", str9.find("z"))
count() Method
Counts how many times a character or substring appears.
str10 = "Learning Python is fun because Python is powerful"
print("Count of 'Python':", str10.count("Python"))
print("Count of 'i':", str10.count("i"))
Why Strings Are Important in Python?
- Used in user input & output
- Essential for data processing
- Widely used in web development, automation, AI, and data science
Key Takeaways
✔ Strings are sequences of characters
✔ Strings are immutable
✔ Indexing starts from 0
✔ Slicing helps extract substrings
✔ Methods like replace(), find(), and count() are heavily used
Important Interview Qs&As
Q1. Are strings mutable in Python?
A1. No, strings are immutable.
Q2. What is slicing in strings?
Extracting part of a string using start and end indexes.
Q3. What does find() return if the value is not found?
find() return if the value is not found?A3. -1
Q4. Difference between find() and count()?
find() and count()?A4.
find()→ returns indexcount()→ returns number of occurrences
Happy Learning !


Leave a Reply