top of page

Python Strings

Updated: Feb 15, 2023

Strings are an integral part of programming, and as a beginner, you must understand how to work and manipulate them. Simply put, a string is a collection of characters you can think of as a word or phrase. With this data type, you can store information such as an individual's name, address, or even a whole paragraph. While they might not seem all that important now, strings are a fundamental building block of programming, and as you learn Python, you'll find yourself using them in various ways.



String Indexing

Indexing is a way to reference an element within a collection, a string in this case, by some positional value with the bounds [0, length-1]. Notice that the range of possible values starts at zero. This is important because the first item's index value in any collection is always zero and never one. A string is a collection just like a list or tuple is (no worries if you don't know what those are), which allows us to extract individual characters by the index position.


Given the following string:

my_str = "The best string"

The bounds of possible index values for the above string are from [0, 14], with the character "T" having an index value of 0 and "g" an index value of 14. The end value of 14 came from counting the total number of characters in the string and subtracting one from the result. To extract a specific character from the string, we surround an index value with the bracket symbol "[index]" and place it to the right of the variable name.


Here's an example of this:

my_str = "The best string"
char = my_str[0]

The character "T" will be stored in the `char` variable.


If the last character is desired, we could do the following:

my_str = "The best string"
char = my_str[14]

While most index values will be specified as positive integers, it is possible to specify negative integer values. In the case of negative values, the bounds on possible values then become [-length, -1] where `-length` corresponds to an index value of 0 and -1 to an index value of 14 if we continue using the `my_str` variable as a reference.


While negative integer values might not seem very beneficial at first glance, they can be a lifesaver when you need the second to last element in a collection and don't know or want to calculate the length. It can also make your code much more readable because you no longer need to subtract values from the length to get at the elements you want.


Here are the two examples from above; only this time, we'll use negative index values to get the desired characters.

my_str = "The best string"
char = my_str[-15]
print(char)

# Output: 'T'
my_str = "The best string"
char = my_str[-1]
print(char)

# Output: 'g'


String Slicing

Slicing builds on the indexing principle discussed above; rather than obtaining one element from the collection (one character from the string), we can extract many or all parts from the collection. The basic syntax for string slicing is as follows:

string[start : end]

Where `string` is the original string, `start` is the starting index (inclusive), and `end` is the stopping index (exclusive). Looking at the `my_str` variable again, we can extract the word "best" from the string with the next slice:

my_str = "The best string"
substring = my_str[4:8]
print(substring)

# Output: "best"

You can also omit the start or end index; sometimes, you can leave out both altogether. Here's an example of leaving out the end index to extract the word "string" from the `my_str` variable:

my_str = "The best string"
substring = my_str[9:]  # Omit the end index
print(substring)

# Output: "string"

Negative index values are also allowed in string slicing. Here's an example of omitting the start index and specifying the end as a negative index value to extract the word "the":

my_str = "The best string"
substring = my_str[:-12]
print(substring)

# Output: "The"

One last thing you can do with string slicing is specifying a step value to select every nth character. The basic syntax for string slicing with a step value is as follows:

string[start: end: step]

The `string`, `start`, and `end` are the same as defined above, and the `step` is the number of elements you want to skip. Here's an example of how you could select every other character in a string:

my_str = "0123456789"
substring = my_str[::2]
print(substring)

# Output: "02468"

By omitting the start and end index, we select the entire string, and the step value selects every two characters starting at first.


We can also use negative values for the step, allowing for some interesting behavior. Here's an example of how you can reverse a string with slicing:

my_str = "0123456789"
substring = my_str[::-1]
print(substring)

# Output: "9876543210"


Common Functions and Methods to Manipulate Strings

Function/Method

Description

len()

​Returns the length of the string

str.upper()

Converts all characters in the string to uppercase

str.lower()

Converts all characters in the string to lowercase

str.title()

Capitalizes the first character in each word

str.replace(old, new)

Replaces all occurrences of the old substring with the new substring

str.strip()

Removes leading and trailing whitespace from the string

str.split(separator)

Splits the string into a list of substrings using the specified separator

str.find(substring)

Returns the index of the first occurrence of the substring, or -1 if not found

str.count(substring)

Returns the number of occurrences of the substring in the string

Note that the "str." in the table is the variable that contains the string you want to manipulate.



Using the `len` Function

The `len` function takes a collection as an argument and returns the number of elements within the collection. In our case, we provide a string (the collection), and the function will count the total number of characters (elements) in the string and return the value. Below is an example of finding the number of characters in a string.

my_str = "The best string"
print(len(my_str))

# Output: 15


Using the `upper` Method

The `upper` method modifies the original string by changing every character in the string to its uppercase form. Below is an example of converting the `my_str` variable to all uppercase characters.

my_str = "The best string"
print(my_str.upper())

# Output: "THE BEST STRING"


Using the `lower` Method

The `lower` method modifies the original string by changing every character in the string to its lowercase form. Below is an example of converting the `my_str` variable to all lowercase characters.

my_str = "The best string"
print(my_str.lower())

# Output: "the best string"


Using the `title` Method

The `title` method modifies the original string by changing the first character of every word in the string to its uppercase form. Here, we define a word as a collection of characters separated by a special character. Below is an example of converting the `my_str` variable to all uppercase characters.

my_str = "The best string"
print(my_str.title())

# Output: "The Best String"


Using the `replace` Method

The `replace` method requires you to provide two substrings: the substring to look for and the substring that will replace it. The method does not modify the original string but returns the result of the replacement. Below is an example of replacing the word "best" with "worst" in the `my_str` variable.

my_str = "The best string"
my_str = my_str.replace("best", "worst")
print(my_str)

# Output: "The worst string"


Using the `strip` Method

The `strip` method removes all white space at the start and end of a string ignoring any spaces in the middle. The method does not modify the original but returns the modified string. Below is an example of how you can use the method to remove white space from a string:

my_str = "   Spa ces    "
my_str = my_str.strip()
print(my_str)

# Output: "Spa ces"


Using the `split` Method

If you're unfamiliar with lists, this method may not make sense. The `split` method splits a string into different elements based on a separator and returns a list containing each separated component. Below is an example of how you can use this method to split a string on every "-" character:

my_str = "s-t-r-i-n-g-s"
my_str = my_str.split("-")
print(my_str)

# Output: ['s', 't', 'r', 'i', 'n', 'g', 's']


Using the `find` Method

The `find` method returns the starting index of the first occurrence of a substring. If the specified substring is not found, the method returns a value of -1. Below are examples of both cases, when the substring can be found and when it can not.

my_str = "The best string"
index = my_str.find("best")
print(index)

# Output: 4

my_str = "The best string"
index = my_str.find("BEST")
print(index)

# Output: -1


Using the `count` Method

The `count` method returns the total number of occurrences a substring was found in a string. If the substring is not found anywhere, a value of zero is returned.

my_str = "aabaaaabababababb"
amount = my_str.count("b")
print(amount)

# Output: 7


3 views0 comments

Recent Posts

See All
bottom of page