Python Lesson 5: Strings

PYTHON

AllComputerss

4/18/20261 min read

python tutorials
python tutorials

Strings are one of the most commonly used data types in Python. They represent sequences of characters and are used to store and manipulate text. Whether you’re working with names, messages, or entire documents, strings are essential.

Creating Strings

Strings can be created by enclosing text in single quotes ('), double quotes ("), or triple quotes (''' or """).

# Examples of strings

single_quote = 'Hello'

double_quote = "World"

triple_quote = '''This is a multiline string.'''

print(single_quote)

print(double_quote)

print(triple_quote)

String Operations

Python provides many ways to work with strings:

  • Concatenation: Combine strings using the + operator.

greeting = "Hello" + " " + "World"

print(greeting) # Output: Hello World

  • Repetition: Repeat strings using the * operator.

laugh = "ha" * 3

print(laugh) # Output: hahaha

  • Indexing and Slicing: Access parts of a string.

text = "Python"

print(text[0]) # Output: P print(text[1:4]) # Output: yth

String Methods

Strings come with built-in methods that make manipulation easy:

message = " hello world "

print(message.upper()) # HELLO WORLD

print(message.lower()) # hello world

print(message.strip()) # hello world (removes whitespace)

print(message.replace("world", "Python")) # hello Python

print(message.split()) # ['hello', 'world']

f-Strings (String Formatting)

Introduced in Python 3.6, f-strings allow you to embed variables directly inside strings.

name = "Alice"

age = 25

print(f"My name is {name} and I am {age} years old.")

Conclusion

Strings are versatile and powerful in Python. From simple text storage to complex formatting and manipulation, they form the backbone of many programs. Mastering strings will make your code more expressive and efficient, preparing you for more advanced topics like regular expressions and text processing.

© 2026 AllComputerss. All rights reserved.