Strings
What is a String?
A string is a data type used in programming. In programming, a string is a sequence of characters. It can be any combination of letters, numbers, symbols, or spaces enclosed in quotation marks. For example, "Hello, World!" is a string.
Strings are commonly used to store and manipulate text data. They allow programmers to work with words, sentences, and other textual information.
Here are a few examples of how strings can be used in programming:
- Storing a user's name or address
- Displaying messages and prompts to users
- Working with textual data such as articles or blog posts
- Performing string operations like searching and replacing
- Storing a telephone number
What is a string?
Python String Concatenation
In Python, string concatenation is the process of combining multiple strings into a single string. There are a few ways to achieve string concatenation in Python:
1. You can use the + operator to concatenate strings together:
str1 = "Hello, "
str2 = "world!"
result = str1 + str2
print(result) # Output: "Hello, world!"
2. You can also use the += operator to concatenate and assign the concatenated string to the left-hand variable:
str1 = "Hello, "
str2 = "world!"
str1 += str2
print(str1) # Output: "Hello, world!"
What is string concatenation?
String Indexing
In Python, string indexing is the process of accessing individual characters within a string using their position (index) in the string. String indexing in Python is zero-based, meaning that the first character has an index of 0, the second character has an index of 1, and so on.
Here's how you can use indexing to access characters in a string:
my_string = "Hello, world!"
# Accessing individual characters using positive indexing
first_char = my_string[0] # 'H'
second_char = my_string[1] # 'e'
last_char = my_string[-1] # '!'
second_last_char = my_string[-2] # 'd'

What is string indexing?
String Slicing
String slicing is a useful feature in Python that allows you to extract parts of a string based on their positions. It is done using the slicing operator ':'. For example, if you have a string called "Hello, world!", you can extract the word "world" by using the slice [7:12].
The syntax for string slicing is as follows:
string[start:stop]
Here, start denotes the starting index of the slice, stop denotes the ending index (exclusive).
Example
Let's look at some examples of string slicing:
string = "Hello, world!"
print(string[7:12]) # Output: "world"
What is string slicing?
String Methods
Python provides a variety of built-in string methods that you can use to manipulate and work with strings. Here are some commonly used string methods:
str.capitalize()
Returns a copy of the string with the first character capitalized and the rest in lowercase.
text = "hello world"
capitalized_text = text.capitalize() # "Hello world"
str.upper() and str.lower()
text = "Hello World"
upper_text = text.upper() # "HELLO WORLD"
lower_text = text.lower() # "hello world"
str.strip(), str.lstrip(), and str.rstrip()
text = " hello "
stripped_text = text.strip() # "hello"
left_stripped = text.lstrip() # "hello "
right_stripped = text.rstrip() # " hello"
Which method is used to convert a string to uppercase?
String Formatting
String formatting in Python allows you to create formatted strings by inserting values into placeholders within a string.
Python provides an easy and readable way of string formatting using f-strings
F-strings provide a concise and readable way to embed expressions inside string literals.
name = "Alice"
age = 30
formatted_string = f"My name is {name} and I am {age} years old."
print(formatted_string) # Output: "My name is Alice and I am 30 years old."
Notice the 'f' at the start of the formatted string?
What is string formatting?
Strings
- What is a String?
- Python String Concatenation
- String Indexing
- String Slicing
- String Methods
- String Formatting