Python

Strings

Strings are a fundamental data type in Python that allow you to store and manipulate text data. Strings allow us to grab small sections of a sequence of characters, a specific index or even passing in a variable to the string to output the variables contents.

Sequences:

In Python, strings are considered to be sequences of characters. This means that you can access individual characters within a string using their position, or index. For instance, the code below is a sequence of 5 characters that make up a string that say ‘Hello’.

str = 'Hello'
Indexing:

To access a specific character within a string, you can use indexing. In Python, indexing begins at 0, so the first character in a string is at index 0, the second character is at index 1, and so on. For example, to access the first character in a string called my_string, you would use my_string[0].

print(str[0]) 
# Outputs 'H'
print(str[2])
# Outputs 'l'

Indexing and Slicing in python always starts with 0.

Slicing:

In addition to accessing individual characters, you can also use slicing to extract a portion of a string. Slicing is performed using the [start:end] syntax, where start is the index of the first character to include in the slice, and end is the index of the character after the last one to include. For example, to extract the first three characters of a string called my_string, you could use my_string[0:3].

print(str[1:3])
# Outputs 'el'
print(str[0:2])
# Outputs 'He'

The end index for slicing is up to but NOT including.

Immutability:

In Python, strings are immutable, which means that once you create a string, you cannot change it. If you want to modify a string, you will need to create a new string with the desired changes. For example, to change the first character of a string called my_string, you would need to create a new string with the desired character and assign it to my_string, like this: my_string = "X" + my_string[1:].

myStr = 'hello'
myStr[2] = 'i'
# Outputs a TypeError: 'str' object does not support item assignment

It’s extremely useful to remember that when an error is thrown, there will always be an error message to help you debug your code. For instance, the error above states that the ‘str’ object doesn’t support Item Assignment, which means that this data type (or object) is immutable.

Concatenation

We can however, use concatenation with strings to create a new string from an already assigned string. What does this mean? Well if you look at the code below, you will see that we can Add a string to another string to create a brand new string. This is only possible because we are assigning a brand new variable to the concatenated strings, meaning that our previous variables are still in their original state.

string1 = 'Hello'
string2 = 'World'
newString = string1 + ' ' + string2
# Outputs 'Hello World'

Python also allows for string formatting, which means we can insert variables into a string in a few different ways. This can be useful when you need to insert dynamic data, such as user input, into a string.

There are several ways to format strings in Python, including:

f-strings

One of the most convenient and powerful ways to format strings in Python is using f-strings. F-strings allow you to embed expressions within a string, which are evaluated and then inserted into the string. For example:

name = "John"
age = 30
print(f"My name is {name} and I am {age} years old.")
str.format()

Another way to format strings in Python is using the str.format() method. This method allows you to specify placeholders in a string, and then pass in values to be inserted into those placeholders. For example:

name = "John"
age = 30
print("My name is {} and I am {} years old.".format(name, age))
% operator

Another option for formatting strings in Python is using the % operator. This operator allows you to specify placeholders in a string, and then pass in values to be inserted into those placeholders. For example:

name = "John"
age = 30
print("My name is %s and I am %d years old." % (name, age))
String methods

Python provides a number of built-in methods for working with strings, such as upper(), lower(), and strip(). These methods can be used to modify and manipulate strings in various ways.

myStr = "Hello World             " 
print(myStr.upper())
# Outputs 'HELLO WORLD             '
print(myStr.lower())
# Outputs 'hello world             '
print(myStr.strip())
# Outputs 'Hello World'
print(myStr.split())
# Outputs ['Hello', 'World']