Python Strings Cheat Sheet
Python Strings Cheat Sheet
Strings Cheat Sheet In Python, individual items in an ordered set of data can indexing
by: Lana Caldarevic - Youtube: Lantech be accessed directly using a numeric index or key value. Negative indices can
This process is referred to as indexing. be used with slicing as well. -1
refers to the last character, -2 the second-to-last, and so
Strings Individual characters in a string can be accessed by on,howjustto asslicewiththesimple indexing. The diagram below shows
Strings are sequences of character data, wrapped inside specifying the string name followed by a number in substring "oob" from the string "foobar"
single, double, or triple quotes. square brackets ([]). using both positive and negative indices:
'This is a string in Python' # string in single quotes String indexing in Python is zero-based: the first
"This is a string in Python" # string in double quotes character in the string has index 0, the next has index 1,
'''This is a string in Python''' # string in triple quotes and so on. The index of the last character will be the
"""This is a string in Python""" # string in triple double- length of the string minus one.
quotes
String indices can also be specified with negative s[-5:-2]
A string literal can be assigned to a variable, and printed numbers, in which case indexing occurs from the end of >>> "oob"
using print() command the string backward: -1 refers to the last character, -2
the second-to-last character, and so on. s[1:4]
message = "Hello World" >>> "oob"
print(message)
s[-5:-2] == s[1:4]
>>> Hello World >>> True