A string in Python is a sequence of characters enclosed within single quotes (' '), double quotes (" "), or triple quotes (''' ''' or """ """).
Strings are used to represent text data such as names, messages, and sentences.
name = "Python"
msg = 'Hello World'
text = """This is a multiline string"""
text = "Python"
print(text[0])
print(text[3])
Output
P
h
print(text[-1])
Output
n
text = "Python"
print(text[1:4])
Output
yth
print(text[0:6:2])
Output
Pto
text = "Python"
text[0] = "J" # Error
👉 Strings cannot be modified directly.
+)print("Hello " + "World")
*)print("Hi " * 3)
print("Py" in "Python")
text = "Python"
for ch in text:
print(ch)
| Method | Description |
|---|---|
lower() |
Converts to lowercase |
upper() |
Converts to uppercase |
strip() |
Removes spaces |
replace() |
Replaces text |
split() |
Splits string |
find() |
Finds position |
text = "hello"
print(text.upper())
| Function | Description |
|---|---|
len() |
Length of string |
max() |
Highest character |
min() |
Lowest character |
| Escape | Meaning |
|---|---|
\n |
New line |
\t |
Tab |
\\ |
Backslash |
print("Hello\nWorld")
name = "Rahul"
age = 20
print(f"My name is {name} and I am {age}")
', ", or """A string is a sequence of characters
Supports:
Cannot be modified after creation
Provides many useful methods for text processing