Skip to article frontmatterSkip to article content

Strings

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:

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
I

You 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: LLI

Remember that the start index is inclusive, while the end index is exclusive.