A string is a sequence of characters enclosed in either single quotes (') or double quotes ("). Strings are used to represent text data in Python.
โจ Creating Stringsยถ
You can create a string by enclosing characters in quotes. Both single and double quotes can be used interchangeably.
my_string = "Hello, World!"
another_string = 'Python is fun!'๐ง String Operationsยถ
โ Concatenationยถ
You can concatenate (combine) strings using the + operator.
greeting = "Hello, " + "World!"
print(greeting) # Output: Hello, World!โ๏ธ Repetitionยถ
You can repeat a string multiple times using the * operator.
laugh = "Ha" * 3
print(laugh) # Output: HaHaHa๐ String Lengthยถ
You can find the length of a string using the len() function.
my_string = "BDI 475"
print(len(my_string)) # Output: 7๐ ๏ธ String Methodsยถ
Strings come with a variety of built-in methods that allow you to manipulate and work with them. Here are some commonly used string methods:
- Changing case:
.lower(),.upper(),.title(),.capitalize(),.swapcase() - Whitespace removal:
.strip(),.lstrip(),.rstrip() - Searching:
.find(),.index(),.count() - Replacing:
.replace() - Splitting and joining:
.split(),.join() - Checking properties:
.startswith(),.endswith(),.isalnum(),.isalpha(),.isdigit()
Weโll explore a few of these methods with examples.
๐ก lower() and ๐ upper()ยถ
These methods convert a string to lowercase or uppercase, respectively.
text = "Data Analytics"
print(text.lower()) # Output: data analytics
print(text.upper()) # Output: DATA ANALYTICS๐งน strip()ยถ
This method removes any leading and trailing whitespace from a string.
text = " I love data! "
print(text.strip()) # Output: "I love data!"โป๏ธ replace()ยถ
text = "I love data!"
print(text.replace("data", "analytics")) # Output: "I love analytics!"This method replaces occurrences of a specified substring with another substring.
โ๏ธ split()ยถ
This method splits a string into a list of substrings based on a specified delimiter (default is whitespace).
text = "I love data analytics"
words = text.split()
print(words) # Output: ['I', 'love', 'data', 'analytics']๐ join()ยถ
This method joins a list of strings into a single string, with a specified separator.
words = ['I', 'love', 'data', 'analytics']
sentence = ' '.join(words)
print(sentence) # Output: "I love data analytics"๐ find()ยถ
This method returns the lowest index of the substring if found in the string. If not found, it returns -1.
text = "I love data analytics"
index = text.find("data")
print(index) # Output: 7๐ Strings as Ordered Collectionsยถ
Although it is not a collection type like lists or dictionaries, strings can be treated as collections of characters, allowing you to iterate over them using loops.
my_string = "ILLINI"
for char in my_string:
print(char)Hereโs the output of the above code:
# Output:
I
L
L
I
N
IYou can access individual characters in a string using indexing, similar to lists.
first_char = my_string[0] # 'I'
last_char = my_string[-1] # 'I'โ๏ธ Slicing a Stringยถ
You can extract a substring from a string using slicing.
my_string = "ILLINI"
substring = my_string[1:4]
print(substring) # Output: LLIRemember that the start index is inclusive, while the end index is exclusive.