Strings

Extract first character in string

Python follows zero-indexing. Indices start from \(0\).

word = 'good'
first = word[0]
print(first)
g

Find length of string

word = 'good'
size = len(word)
print(size)
4

Extract last character in string

Python allows negative indexing. An index of \(-1\) corresponds to the last character in a string, the index \(-2\) corresponds to the penultimate character, and so on.

word = 'good'
last = word[-1]
print(last)
d
word = 'good'
size = len(word)
last = word[size - 1]

Since Python follows negative indexing, the last index is len(word) - 1 and not len(word).

Extract first five letters from string

word = 'extracting'
first_five = word[0:5]
print(first_five)
extra

The start-index defaults to \(0\) when it is left unspecified.

word = 'extracting'
first_five = word[:5]
print(first_five)
extra

Extract last three letters from string

End-index defaults to len(string) when it is left unspecified,

word = 'extracting'
last_three = word[-3:]
print(last_three)
ing
word = 'extracting'
last_three = word[len(word) - 3:]
print(last_three)
ing

Exclude first and last letter from string

word = 'bores'
out = word[1:-1]
print(out)
ore

Determine if a string is a substring of another

str_1 = 'fume'
str_2 = 'perfumes'
outcome = str_1 in str_2
print(outcome)
True

String together multiple strings

roll_number in the code-below is an f-string or a formatted string. The values of the variables enclosed within {} will get substituted.

year = 22
term = 'f2'
roll = '001023'
domain = 'ds.study.iitm.ac.in'
roll_number = f'{year}{term}{roll}@{domain}'
print(roll_number)
22f2001023@ds.study.iitm.ac.in

Replicate string \(n\) times

word = 'abc'
n = 5
word_repeat = word * n
print(word_repeat)
abcabcabcabcabc

Reverse string

The third argument after colon is the step-size. Here, the step size is \(-1\).

word = '123456789'
rev = word[::-1]
print(rev)
987654321

Convert string to lower case

word = 'GREAT'
word = word.lower()
print(word)
great

Note that strings are immutable. This means that the string 'GREAT' cannot be changed. When 'GREAT'.lower() is called, the string 'great' is returned. This new string is reassigned to word. To understand this better, consider the following snippet:

word = 'GREAT'
word_lower = word.lower()
print(word, word_lower)
GREAT great

Convert string to upper case

word = 'great'
word = word.upper()
print(word)
GREAT

Count frequency of character in string

word = 'babble'
char = 'b'
freq = word.count(char)
print(freq)
3

Replace character in string

word = 'ph0t0'
char = '0'
to = 'o'
word = word.replace(char, to)
print(word)
photo

Capitalize string

name = 'krishna'
name = name.capitalize()
print(name)
Krishna

Check if string is in upper case

word_1 = 'GREAT'
word_2 = 'great'
print(word_1.isupper())
print(word_2.isupper())
True
False

Check if string is in lower case

word_1 = 'GREAT'
word_2 = 'great'
print(word_1.islower())
print(word_2.islower())
False
True

Check if string has only letters

word_1 = 'abcd'
word_2 = 'abcd1234'
print(word_1.isalpha())
print(word_2.isalpha())
True
False

Check if string has only numbers

word_1 = '1234'
word_2 = 'abcd1234'
print(word_1.isdigit())
print(word_2.isdigit())
True
False

Check if string has only letters and numbers

word_1 = 'abcd1234'
word_2 = 'abcd1234!@#$'
print(word_1.isalnum())
print(word_2.isalnum())
True
False

Find index of character that is present in string

word = 'abcde'
char = 'c'
index = word.index(char)
print(index)
2

index will return the index if the character is present. It will throw an error if the character is not present. For an error-free way of handling this, check the next how-to.

Find index of character that may not be present in string

word = 'abcde'
char = 'f'
index = word.find(char)
print(index)
-1

find will return the index if the character is present. It will return \(-1\) if the character is not present.

Remove whitespaces from beginning and end of string

string = '\n  white \n'
print(string)
string = string.strip()
print(string)

  white 

white

\n', '\t' and ' ' are whitespaces. '\n’ is the newline character, '\t' is the tab character.

Check if string ends with another string

word = 'extracting'
end = 'ing'
print(word.endswith(end))
True

Check if word-1 comes before word-2 in a dictionary

word_1 = 'erase'
word_2 = 'ease'
print(word_1 < word_2)
False