What is a String?
A string is a sequence of characters used to store text. Strings are written inside single or double quotes.
Examples:
name = "Python" msg = 'Hello World'
Important Points about Strings
- Strings store text data
- Strings are immutable (cannot be changed)
- Indexing starts from 0
String Input
name = input("Enter your name: ")
String Length
len("Python") # 6
Indexing
Each character in a string has an index.
text = "Python" text[0] # P text[-1] # n
Slicing
text[0:3] # Pyt text[2:] # thon text[::-1] # Reverse string
Common String Methods
upper()– convert to uppercaselower()– convert to lowercasecapitalize()– capitalize first letterreplace()– replace part of stringsplit()– split string into words
Looping Through a String
for ch in "Python":
print(ch)
Key Reminder
- Strings cannot be modified directly
- Always create a new string when changing content
Now attempt the quiz below.
Select the correct answer and click Submit.
0 Comments