Working with Strings
Index, slice, search and transform text.
A string is a sequence of characters. You can look at single characters by their position, called an index, starting from 0. Negative indexes count from the end.
Python
word = "Python"
print(word[0])
print(word[5])
print(word[-1])
print(len(word))
Output
P n n 6
Slicing
text[start:stop] gives the part from start up to, but not including, stop. Leave out either number to go from the beginning or to the end.
Python
s = "Hello, world"
print(s[0:5])
print(s[7:])
print(s[:5])
print(s[::-1]) # a step of -1 reverses the string
Output
Hello world Hello dlrow ,olleH
Useful string methods
Strings come with many built-in methods. None of them change the original string; they return a new one.
Python
title = " learning python is fun "
print(title.strip())
print(title.strip().title())
print(title.upper().strip())
print(title.count("n"))
print(title.replace("fun", "useful").strip())
print("python" in title)
Output
learning python is fun Learning Python Is Fun LEARNING PYTHON IS FUN 4 learning python is useful True
Splitting and joining
split() breaks a string into a list of pieces, and join() glues a list back together.
Python
csv_line = "Asha,21,Pune"
parts = csv_line.split(",")
print(parts)
words = ["code", "every", "day"]
print(" ".join(words))
print("-".join(words).upper())
Output
['Asha', '21', 'Pune'] code every day CODE-EVERY-DAY