[ SRE_ARENA ]
PYTHON STRINGS MASTERCLASS

Strings are Arrays in Disguise.

Implementation Code: 1. Slicing & Modifying
PYTHON
s = "Hello, World!"
print(len(s)) # 13

# Slicing [start:end]
print(s[2:5]) # llo (End is exclusive)
print(s[:5])  # Hello (Starts from 0)
print(s[-6:-1]) # World (Negative indexes)

# Modifying
print(s.upper()) # HELLO, WORLD!
print(s.replace("H", "J")) # Jello, World!
print(s.split(",")) # ['Hello', ' World!']
DOWNLOADING CONCEPT: 1. SLICING & MODIFYING...
  • ↳Strings in Python are surrounded by either single quotation marks, or double quotation marks.
  • ↳Multiline strings use three quotes: '''text'''.
  • ↳Like many other popular programming languages, strings in Python are arrays of bytes representing unicode characters.
  • ↳You can access elements using square brackets: print(a[1]).
  • ↳You can return a range of characters by using the slice syntax [start:end].
  • ↳The end index is NOT included.
  • ↳You can use negative indexes to start the slice from the end of the string.

↳Python has a set of built-in methods that you can use on strings, like upper(), lower(), replace(), and split().

▲ LIVE INCIDENTA script is trying to parse an IP address from a messy log line.
sre@arena:~/python-strings
sre@arena:~$ python -c "log = ' ERROR: 192.168.1.5 connected\n'; print(log.strip().split(' ')[1])"