Sahaj.blogs
16 Aug 2023 · StudyPhase 1 · Python

Python · Strings: indexing, slicing, methods

Strings as immutable sequences: indexing, negative indexes, slicing with steps, and the common methods.

A string is an immutable sequence of Unicode characters. Immutable means s[0] = "X" raises an error, and every “changing” method actually returns a new string.

Indexing starts at 0. Negative indexes count from the end, so s[-1] is the last character.

Slicing is s[start:stop:step]. start is included and stop is excluded, and any of the three can be left out. s[::-1] reverses a string.

The methods I’ll use most:

methoddoes
strip()removes whitespace at both ends
lower() / upper()changes case
split(sep)turns a string into a list of parts
"sep".join(list)the reverse of split
replace(a, b)returns a copy with replacements
find(x) / insearches
s = "  Pandit Deendayal Energy University  "
t = s.strip()
print(repr(t))
print(t[0], t[-1], t[7:16], t[::-1][:10])

words = t.split()
print(words, len(words))
print("".join(w[0] for w in words))   # initials
print(t.lower().replace("university", "Univ."))
print("Energy" in t, t.find("Energy"))

try:
    t[0] = "p"
except TypeError as e:
    print("TypeError:", e)

Output:

'Pandit Deendayal Energy University'
P y Deendayal ytisrevinU
['Pandit', 'Deendayal', 'Energy', 'University'] 4
PDEU
pandit deendayal energy Univ.
True 17
TypeError: 'str' object does not support item assignment

#python#strings

Sources

  1. An Informal Introduction to Python: text — Python Software Foundation
  2. String methods — Python Software Foundation
  3. Common sequence operations — Python Software Foundation