Skip to main content

String

  • immutable

Initialize

s = "Hello World"

Access

s[0] # 'H'
s[-1] # 'd'

Delete

del s[2] # Error ❌
s = s[0:2] + s[3:]

String formatting

s = f"Hello {name}!"

# Rounding off Integers
s = "{0:.2f}".format(1/6) # 0.17

s = "%s, %s!"%("Hello", "world")
s = "{} {} {}".format('Geeks', 'For', 'Life')
s = "{1} {0} {2}".format('Geeks', 'For', 'Life')
s = "{l} {f} {g}".format(g='Geeks', f='For', l='Life')

Others

ord('a') # 97
chr(97) # a

len(s) # length

s[1:3] # slicing
s[::-1] # reverse

"a" in s
"a" not in s

String methods

s.strip()
s.strip("-")

s = "ILoveDSA"
s.islower() # False
s.isupper() # False
s.lower() # ilovedsa
s.upper() # ILOVEDSA

arr = s.split(" ")
s = ''.join(arr)

s.isalnum() # alphanumeric
s.isalpha() # alphabets
s.isdigit() # digits

s.count("Hello") # count
s.endswith(".") # ends with
s.find("l") # first index of -> -1 if not present
s.index("l") # first index of -> Error if not present ❌

s.lstrip() # removes leading characters

s.replace("hello", "hi")
tip

If there are a lot of modifications required then choose an array and return it as a string