Strings
A string is a sequence of characters used to represent text in Python. Strings are commonly used for names, messages, addresses, course names, user input, and other text-based information.
What is a String?
A string is text enclosed inside quotes. Python supports both single quotes and double quotes.
name = "Samadhan"
course = 'Python'
print(name)
print(course)
Output
Samadhan
Python
String Concept
Samadhan
" "
"Samadhan"
Single and Double Quotes
Python allows strings to be created using either single quotes or double quotes.
name1 = 'Samadhan'
name2 = "Samadhan"
print(name1)
print(name2)
Both variables contain string values.
Multiline Strings
Triple quotes can be used when a string needs to span multiple lines.
message = """Welcome to
CIIT Training Institute.
Learn Python step by step."""
print(message)
Output
Welcome to
CIIT Training Institute.
Learn Python step by step.
String Indexing
Each character in a string has a position called an index. Python string indexing starts from 0.
name = "SAM!"
print(name[0])
print(name[1])
print(name[2])
print(name[3])
Output:
S
A
M
!
Negative Indexing
Python also supports negative indexing. The last character has index -1.
name = "Python"
print(name[-1])
print(name[-2])
Output
n
o
String Slicing
Slicing is used to extract a portion of a string. The syntax is:
string[start:end]
The start index is included, while the end index is excluded.
text = "Python"
print(text[0:3])
print(text[2:6])
Output
Pyt
thon
Finding String Length
The len() function returns the number of characters in a string.
name = "Samadhan"
print(len(name))
Output
8
String Concatenation
Concatenation means joining two or more strings. The + operator can be used to join strings.
first_name = "Samadhan"
last_name = "Patole"
full_name = first_name + " " + last_name
print(full_name)
Output
Samadhan Patil
String Repetition
The * operator can repeat a string multiple times.
message = "Hi "
print(message * 3)
Output
Hi Hi Hi
Changing String Case
Python provides methods such as upper() and lower() to change the case of a string.
text = "Python Programming"
print(text.upper())
print(text.lower())
Output
PYTHON PROGRAMMING
python programming
Removing Extra Spaces
The strip() method removes leading and trailing whitespace from a string.
name = " Samadhan "
print(name.strip())
Output
Samadhan
Replacing Text
The replace() method replaces one piece of text with another.
course = "Learn Java"
new_course = course.replace("Java", "Python")
print(new_course)
Output
Learn Python
Finding Text
The find() method returns the index of the first occurrence of a substring.
text = "Python Programming"
position = text.find("Program")
print(position)
Output:
7
Checking Text using in
The in operator can check whether a particular text exists inside another string.
course = "Python Full Stack"
print("Python" in course)
print("Java" in course)
Output
True
False
Splitting a String
The split() method divides a string into a list based on a separator.
courses = "Python,Java,C#"
result = courses.split(",")
print(result)
Output
['Python', 'Java', 'C#']
Joining Strings
The join() method combines multiple strings into one string.
courses = ["Python", "Java", "C#"]
result = ", ".join(courses)
print(result)
Output
Python, Java, C#
String Formatting using f-Strings
f-strings provide a simple way to insert variables directly into a string.
name = "Samadhan"
course = "Python"
message = f"{name} is learning {course}"
print(message)
Output
Samadhan is learning Python
Strings are Immutable
Python strings are immutable. This means individual characters of an existing string cannot be changed directly.
Instead, a new string is created when a modified value is required.
name = "Python"
name = "Python Programming"
print(name)
Output:
Python Programming
Example ππ€
Let's create a simple student profile using strings at CIIT Training Institute π©π¨βπ«...!.
student_name = "Pradnya"
course = "Python Full Stack"
institute = "CIIT Training Instituteπ©π¨βπ«..."
message = (
f"Student: {student_name}\n"
f"Course: {course}\n"
f"Institute: {institute}"
)
print(message)
Output
Student: Pradnya
Course: Python Full Stack
Institute: CIIT Training Instituteπ©π¨βπ«...
CIIT Learning Point
Practice string indexing, slicing, concatenation, formatting, and commonly used methods such as upper(), lower(), strip(), replace(), split(), and join(). These skills are frequently used while processing real-world application data.
CIIT Practice Task
Create a Python program that stores your name, city, course, and institute name. Display them using an f-string.
name = "Om"
city = "Pune"
course = "Python"
institute = "CIIT Training Institute π©π¨βπ«..."
print(
f"My name is {name}. "
f"I live in {city}. "
f"I am learning {course} at {institute}."
)
Output:
My name is Om. I live in Pune. I am learning Python at CIIT Training Institute π©π¨βπ«....
Try changing the values and observe the output.
Summary :
Strings are used to store and process text in Python. You learned how to create strings, access characters using indexing, extract text using slicing, combine strings, search and replace text, split and join strings, and format dynamic text using f-strings.