Master Python Programming From Scratch

Clear, interactive, and structured coding lessons designed for absolute beginners.

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.

CIIT Training Institute : Strings are used in almost every application. From student names and course information to API responses and database records, handling text correctly is an essential Python skill.

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

Text
Samadhan
↓
Quotes
" "
↓
Python String
"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.

Index
0
1
2
3
4

Character
S
A
M
!
😊
name = "SAM!"

print(name[0])
print(name[1])
print(name[2])
print(name[3])

Output:

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:

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:

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:

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.