Lecture 6 : How to Use Strings in Python : Python full Course - 2026

Опубликовано: 21 Март 2026
на канале: Tech With Machines
12
1

#pythonstrings #pythonstring #stringsinpython


Python Strings
A string in Python is a sequence of characters enclosed within single quotes (') or double quotes ("). Python also allows multi-line strings using triple quotes (''' or """).

Creating Strings
Strings can be created by enclosing characters in quotes.

Examples:

python
Copy code
Single-line string
single_line = "Hello, World!"
single_line2 = 'Hello, Python!'

Multi-line string
multi_line = """This is
a multi-line
string"""
Accessing String Elements
Strings in Python are indexed, with the first character at index 0. You can use both positive and negative indices to access elements.

Examples:

python
Copy code
greeting = "Hello, World!"

Positive index
print(greeting[0]) # Output: H
print(greeting[7]) # Output: W

Negative index (starts from the end)
print(greeting[-1]) # Output: !
print(greeting[-6]) # Output: W
Slicing Strings
You can extract parts of a string (called a substring) using slicing.

Syntax:

python
Copy code
string[start:end:step]
start: The index where the slice starts (inclusive).
end: The index where the slice ends (exclusive).
step: The interval between each character to include (optional).
Examples:

python
Copy code
text = "Hello, World!"

Extracting a substring from index 0 to 4
print(text[0:5]) # Output: Hello

Extracting a substring from index 7 to the end
print(text[7:]) # Output: World!

Using step to extract every second character
print(text[::2]) # Output: Hoo ol!
String Operations
Concatenation: Combine strings using +.

python
Copy code
name = "Alice"
greeting = "Hello, " + name
print(greeting) # Output: Hello, Alice
Repetition: Repeat a string using *.

python
Copy code
repeated = "Python! " * 3
print(repeated) # Output: Python! Python! Python!
Length: Get the length of a string using len().

python
Copy code
length = len("Hello")
print(length) # Output: 5
Check Membership: Check if a substring exists within a string using in.

python
Copy code
phrase = "Hello, World!"
print("Hello" in phrase) # Output: True
print("Python" in phrase) # Output: False
String Methods
Strings have many built-in methods that allow you to manipulate and analyze them. Some of the most common methods include:

Method Description
upper() Converts all characters to uppercase.
lower() Converts all characters to lowercase.
capitalize() Capitalizes the first letter of the string.
title() Capitalizes the first letter of each word.
strip() Removes leading and trailing whitespace.
replace(old, new) Replaces all occurrences of a substring with a new one.
split(delim) Splits the string into a list based on a delimiter.
find(substring) Returns the index of the first occurrence of a substring (or -1 if not found).
count(substring) Counts how many times a substring appears in the string.
join(iterable) Joins a list of strings into one string.
Examples:

python
Copy code
text = " Hello, World! "

Removing whitespace
clean_text = text.strip()
print(clean_text) # Output: "Hello, World!"

Changing case
print(text.upper()) # Output: " HELLO, WORLD! "
print(text.lower()) # Output: " hello, world! "

Replacing substrings
replaced = text.replace("World", "Python")
print(replaced) # Output: " Hello, Python! "

Splitting a string
words = "apple,banana,cherry".split(",")
print(words) # Output: ['apple', 'banana', 'cherry']

Finding a substring
position = text.find("World")
print(position) # Output: 9 (index of "World")

Counting occurrences
count = "apple apple apple".count("apple")
print(count) # Output: 3
Escape Characters
To include special characters in strings, you can use escape sequences (denoted by a backslash \).

Escape Sequence Meaning
\n Newline
\t Tab
\\ Backslash
\' Single quote
\" Double quote
\r Carriage return
Example:

python
Copy code
text = "Hello\nWorld\tPython"
print(text)
Output:
Hello
World Python
F-Strings (Formatted Strings)
Starting from Python 3.6, f-strings provide a concise way to embed expressions inside string literals.

Syntax:

python
Copy code
f"Some text {expression}"
Example:

python
Copy code
name = "Alice"
age = 25
greeting = f"Hello, my name is {name} and I am {age} years old."
print(greeting) # Output: Hello, my name is Alice and I am 25 years old.
String Immutability
Strings in Python are immutable, meaning that their contents cannot be changed after they are created. However, you can perform operations like concatenation, slicing, or reassignment to create new strings based on the original one.

Summary Table
Operation Example Output
Length len("Hello") 5
Uppercase "hello".upper() "HELLO"
Lowercase "HELLO".lower() "hello"
Replace "hello".replace("e", "a") "hallo"
Split "apple,banana".split(",") ['apple', 'banana']
Find "hello".find("e") 1
Concatenate "Hello" + " World!" "Hello World!"
Strings are an essential part of working with text in Python. Let me know if you need more details or specific examples!