Last updated: Apr 10, 2024
Reading timeยท4 min
Use string slicing to get the first N characters of a string, e.g.
first_n = string[:n]
.
The slicing operation will return a new string that starts at index 0
and
contains the first N characters of the original string.
string = 'bobbyhadz.com' first_char = string[0] print(first_char) # ๐๏ธ b # โ get the first 2 characters of a string first_2 = string[:2] print(first_2) # ๐๏ธ bo # โ get the first 3 characters of a string first_3 = string[:3] print(first_3) # ๐๏ธ bob # โ get the first 5 characters of a string first_5 = string[:5] print(first_5) # ๐๏ธ bobby
We used string slicing to get the first N characters of a string.
The syntax for string slicing is my_str[start:stop:step]
.
The start
index is inclusive, whereas the stop
index is exclusive (up to,
but not including).
0
, and the last character has an index of -1
or len(my_str) - 1
.The slice string[:2]
starts at index 0
and goes up to, but not including
index 2
.
string = 'bobbyhadz.com' first_2 = string[:2] print(first_2) # ๐๏ธ bo
In other words, the slice returns the characters at index 0
and 1
.
You could also explicitly specify a start
index of 0
, but if you don't, a
value of 0
is implied.
string = 'bobbyhadz.com' first_2 = string[0:2] print(first_2) # ๐๏ธ bo first_2 = string[:2] print(first_2) # ๐๏ธ bo
The two examples in the code sample achieve the same result.
n
is greater than the string's length, the entire string is returnedIf the specified value of n
is greater than the string's length, the entire
string is returned.
string = 'bobbyhadz.com' print(string[:100]) # ๐๏ธ bobbyhadz.com
The string doesn't contain 100
characters, so a slice that represents the
entire string is returned.
The string slicing operation won't cause an error if used with an empty string.
string = '' print(repr(string[:100])) # ๐๏ธ ''
The string in the example is empty, so trying to get its first N characters returns an empty string.
If you need to get the remainder of the string, use a start
index of n
.
string = 'bobbyhadz.com' n = 5 # โ Get the first 5 characters of a string first_5 = string[:n] print(first_5) # ๐๏ธ bobby # โ Get the characters after the first 5 after = string[n:] print(after) # ๐๏ธ hadz.com
The slice string[:5]
starts at index 0
and goes up to, but not including
index 5
.
The slice string[5:]
starts at index 5
and goes to the end of the string.
start
indexes are inclusive, whereas stop
indexes are exclusive (up to, but not including).If you need to get the last N characters of a string, use a negative start
index.
string = 'bobbyhadz.com' last_2 = string[-2:] print(last_2) # ๐๏ธ 'om'
Negative indices can be used to count backward, e.g. string[-1]
returns the
last character in the string and string[-2]
returns the second to last
character.
The slice string[-2:]
starts at the second to last character in the string and
goes to the end.
You can also use a for loop to get the first N characters of a string.
This is a three-step process:
for
loop to iterate over the string.string = 'bobbyhadz.com' first_n = '' n = 5 for char in string: if n < 1: break n -= 1 first_n += char print(first_n) # ๐๏ธ 'bobby'
We used a for
loop to iterate over the string.
On each iteration, we check if the value stored in the n
variable is less than
1
.
If the condition is met, we exit the for
loop.
The break statement breaks out of the
innermost enclosing for
or while
loop.
Otherwise, we use the addition (+) operator to add the current character to the new string.
The +=
operator is a shorthand for first_n = first_n + char
.
Similarly, the -=
operator is a shorthand for n = n - 1
.
Note that this approach is much less efficient than using string-slicing directly.
You can also create a reusable function that returns the first N characters of a string.
def get_first_n(string, n): return string[:n] string = 'bobbyhadz.com' print(get_first_n(string, 2)) # ๐๏ธ bo print(get_first_n(string, 3)) # ๐๏ธ bob print(get_first_n(string, 5)) # ๐๏ธ bobby
The function takes a string and n
as parameters and returns the first n
characters of the supplied string.
You can learn more about the related topics by checking out the following tutorials: