| < previous | Table of Contents | next > |
6.2 STRING OPERATIONS
String operations allow us to manipulate, evaluate, and transform strings.
CONCATENATION
Concatenation allows us to join two or more strings together by using the + operator.
greeting = "Hello" + " " + "World!" print(greeting) # This will print out 'Hello World!' to the console
REPETITION
We can repeat a string of a specified number of times using the * operator.
greeting = "Hello! " * 2 print(greeting) #Ouputps Hello! Hello!
INDEXING
We can access individual characters in a string by their position. Strings in Python are zero-indexed, meaning that the first character of a string is at index zero, the second one is at index one, and so forth. NB! The indexes have to be integers.
greeting = "Hello World!" print(greeting[0]) #Outputs the character ‘H’ from the string greeting.
Python also supports negative indexing where -1 refers to the last element, -2 to the second to last, etc.
greeting = "Hello World!" first_letter = greeting[0] # 'H' last_letter = greeting[-1] # '!'
SLICING
Slicing enables us to extract a part of a string, a substring, by specifying a range we want to extract.
slice = string[start:stop:step]
startis the index where the slice starts. It’s inclusive of the specified index. Ifstartis omitted, then slicing starts from the beginning by default.stopis the index where the slice ends. Note that it is exclusive, meaning that thestopindex you’ve specified will not be included in the slice. However, ifstopis omitted then slicing goes up to and including the final character of the string.- Lastly,
stepis the step count between elements in the slice. For example, if thestepis 2, we skip every other character. Thestepcan also take positive and negative values. By default, the step is 1. - Note that all the items in the brackets are optional.
In this example, we want to extract the word "are" from the full string, we can do so by accessing the elements through slicing according to their index test_string[4:8]. The step can also be negative, which allows us to do cool things like reversing a string test_string[::-1].

THE IN OPERATOR
in is a boolean operator that allows you to determine whether a value exists within a string. It returns True if the specified value is found and False if it’s not.
Here, we try to see if the word "you" exists within the string. We can use the in-operator to do so. We can see that the program outputs True meaning that the word "you" is present in the string.

Note that the in operator is case sensitive, meaning that the case of the characters must match exactly the substring you’re trying to check for. As you can see, as we changed the word to start with a capital letter "You", now that we check to see if the word is in the string, the result is False due to case sensitivity.

The in operator can also be combined with not for negation. not in allows you to check if a substring does not exist within another string. As you can see the result of our previous example is now True.

KEY THINGS TO REMEMBER
- Concatenation joins two or more strings into one using the
+operator. - Repetition repeats a string desired number of times using the
*operator. - Characters are zero-indexed, meaning the first character is at index 0.
- Slicing allows us to access a substring of a string.
- The
inoperator checks if a substring exists within another string and returns eitherTrueorFalse.not incan be used for negation.
SELF-CONTROL EXERCISES
| < previous | Table of Contents | next > |