Python - String
Introduction to String
A string is one of the most commonly used data types.
A string is an ordered and immutable sequence of characters enclosed within quotes.
Strings are used to store text data such as names, messages, addresses, passwords, etc.
Example:
name = "Rajat"
Here, "Rajat" is a string.
Characteristics of String
- ✔ Ordered (characters have fixed positions)
- ✔ Indexed (we can access characters using index)
- ✔ Immutable (cannot be changed after creation)
- ✔ Allows duplicate characters
- ✔ Supports slicing
Creating a String
Python allows three ways to create strings:
1. Single Quotes
a = 'Hello'
2. Double Quotes
b = "Python"
3. Triple Quotes (Multiline String)
c = """This is
a multiline
string"""
Indexing in String
Each character in a string has a position number called index.
Index always starts from 0.
a = "Python"
Character P y t h o n
Index 0 1 2 3 4 5
Accessing characters:
print(a[0]) # P
print(a[3]) # h
Negative Indexing
Negative index starts from -1 (from right side).
print(a[-1]) # n
print(a[-2]) # o
String Slicing
Slicing is used to extract part of a string.
Syntax:
string[start : end : step]
Example:
a = "Python"
print(a[0:4]) # Pyth
print(a[2:]) # thon
print(a[:4]) # Pyth
print(a[::-1]) # nohtyP
Immutability of String
Strings cannot be changed after creation.
Wrong:
a = "Hello"
a[0] = "Y" # Error
Correct:
a = "Hello"
a = "Y" + a[1:]
print(a)
String Operators
1. Concatenation (+)
Used to join strings.
a = "Hello"
b = "World"
print(a + " " + b)
Output:
Hello World
2. Repetition (*)
print("Hi " * 3)
Output:
Hi Hi Hi
Membership Operators
Used to check if character or word exists in string.
a = "Python"
print("P" in a)
print("z" not in a)
Built-in String Functions
len()
Returns length of string.
a = "Python"
print(len(a))
max() and min()
Return highest and lowest ASCII value character.
a = "Python"
print(max(a))
print(min(a))
Important String Methods
1. lower()
Converts to lowercase.
a = "HELLO"
print(a.lower())
2. upper()
a = "hello"
print(a.upper())
3. strip()
Removes spaces from both sides.
a = " hello "
print(a.strip())
4. replace()
a = "Hello World"
print(a.replace("World", "Python"))
5. split()
Converts string into list.
a = "apple,banana,orange"
print(a.split(","))
6. join()
Joins list into string.
a = ["apple", "banana", "orange"]
print(", ".join(a))
7. find()
Returns index of substring.
a = "Python Programming"
print(a.find("Pro"))
If not found → returns -1.
8. count()
a = "banana"
print(a.count("a"))
String Formatting
Using format()
name = "Rajat"
age = 25
print("My name is {} and I am {}".format(name, age))
Using f-string (Modern Method)
name = "Rajat"
age = 25
print(f"My name is {name} and I am {age}")
Escape Characters
Escape Meaning
\n New line
\t Tab
\ Backslash
' Single quote
" Double quote
Example:
print("Hello\nWorld")
Looping Through String
a = "Python"
for i in a:
print(i)
Practical Programs
1. Reverse a String
a = "Python"
print(a[::-1])
2. Check Palindrome
a = "madam"
if a == a[::-1]:
print("Palindrome")
else:
print("Not Palindrome")
3. Count Vowels
a = "Python Programming"
vowels = "aeiouAEIOU"
count = 0
for i in a:
if i in vowels:
count += 1
print(count)
Advantages of String
- Easy to use
- Powerful built-in methods
- Used in almost every application
- Essential for text processing
© KCSDOCS | All Rights Reserved