Strings and String Methods
Strings
By Kelvin Kiprono in Python
September 2, 2026
Strings and Strings Methods
Collections of text in Python are called strings. Special functions called string methods are used to manipulate strings. There are string methods for changing a string from lowercase to uppercase, removing whitespace from the beginning or end of a string, replacing parts of a string with different text, and much more.
Strings are used to represent text
Strings have three important properties:
- Strings contain individual letters or symbols called characters.
- Strings have a length, defined as the number of characters the string contains.
- Characters in a string appear in a sequence, which means that each character has a numbered position in the string.
Determine the Length of a String
The number of characters contained in a string, including spaces, is called the length of the string. For example, the string “abc” has a length of 3, and the string “Don’t Panic” has a length of 11.
Python has a built-in len() function that you can use to determine the length of a string.
len("abcde")
## 5
name= "kelvin"
print(len(name))
## 6
Concatenation, Indexing, and Slicing
- Concatenation, which joins two strings together
- Indexing, which gets a single character from a string
- Slicing, which gets several characters from a string at once
String Concatenation
You can combine, or concatenate, two strings using the + operator:
name1="Monica"
name2="Williams"
full_name= name1 + " "+name2
full_name
## 'Monica Williams'
Here, you use string concatenation twice on the same line. First, you concatenate first name with " " to ensure a space appears after the first name in the final string.
String Indexing
Each character in a string has a numbered position called an index.
You can access the character at the nth position by putting the number n between two square brackets ([ ]) immediately after the string:
color="Yellow"
color[1]
## 'e'
String Slicing
Suppose you need a string containing just the first three letters of the string “Yes Sir”. You could access each character by index and concatenate them like this:
condition ="Yes Sir"
first_three= condition[0] + condition[1]+condition[2]
first_three
## 'Yes'
You can extract a portion of a string, called a substring, by inserting a colon between two index numbers set inside square brackets like this:
condition[0:3]
## 'Yes'
If you omit the first index in a slice, then Python assumes you want to start at index 0:
condition[:3]
## 'Yes'
The slice [:3] is equivalent to the slice [0:3], so condition[:3] returns the first three characters in the string
Similarly, if you omit the second index in the slice, then Python assumes you want to return the substring that begins with the character whose index is the first number in the slice and ends with the last character in the string:
condition[3:]
## ' Sir'
Strings Are Immutable
Strings are immutable, which means that you can’t change them once you’ve created them. For instance, see what happens when you try to assign a new letter to one particular character of a string:
word = "Mountain"
word[0]="F"
## TypeError: 'str' object does not support item assignment
Python throws a TypeError and tells you that str objects don’t support item assignment.If you want to alter a string, then you must create an entirely new string.
Converting String Case
To convert a string to all lowercase letters, you use the string’s .lower() method. This is done by tacking .lower() onto the end of the string itself:
"LEONARD".lower()
## 'leonard'
The dot (.) tells Python that what follows is the name of a method the lower() method in this case.
The opposite of .lower() is .upper(), which converts every character in a string to uppercase:
"leonard".upper()
## 'LEONARD'
Removing Whitespace From a String
Whitespace is any character that is printed as blank space. This includes things like spaces and line feeds, which are special characters that move output to a new line.
Sometimes you need to remove whitespace from the beginning or end of a string. This is especially useful when working with strings that come from user input, which may include extra whitespace characters by accident.
There are three string methods that you can use to remove whitespace from a string:
- .rstrip()
- .lstrip()
- .strip()
.rstrip() removes whitespace from the right side of a string:
name="Kelvin Kiprono "
name
## 'Kelvin Kiprono '
name.rstrip()
## 'Kelvin Kiprono'
.lstrip() works just like .rstrip(), except that it removes whitespace from the left-hand side of the string:
To remove whitespace from both the left and the right sides of the string at the same time, use .strip():
word=" hey did you call him "
word
## ' hey did you call him '
word.strip()
## 'hey did you call him'