String CheatSheet
Complete Python String Cheatsheet with clear examples for quick revision
3
15
.replace(old, new)
str1 = 'python is python and python is good.'
result = str1.replace('Python', 'java') # Note: case-sensitive
# result: 'python is python and python is good.' (no change due to case)str1 = 'python is python and python is good'
result = str1.replace('python', 'java', 2)
# 'java is java and python is good.'16
17
19
21
22
23
32
String Formatting ( f" " )
# Basic Example
name = "Ayush"
age = 61
print(f"My name is{name} and age is{age}") # My name is Ayush and my age is 61# Expression inside f-string
a = 10
b = 5
print(f"Sum is{a+b}")String Slicing
str1 = "Programming"str1[::] # 'Programming'str1[0:6] # 'Progra'Escape Characters
Last updated