What is a String ?
A string is a sequence of characters (letters, numbers, symbols, spaces, etc.) enclosed within quotes.
In simple words:
Text data = String
How to create a string
You can use:
- Single quotes
' ' - Double quotes
" " - Triple quotes
''' '''or""" """(for multi-line)
name = "Silambu"
city = 'Chennai'
msg = """This is
multiple line string"""
- String is immutable → you cannot change characters directly
- Each character has an index (position)
text = "Python"
print(text[0]) # P
print(text[1]) # y
Common string operations
name = "silambu"
print(name.upper()) # SILAMBU
print(name.capitalize()) # Silambu
print(name.lower()) # silambu
print(len(name)) # 7
print("My name is " + name) #My name is Silambu
Both first & last letter capital
name = "silambu"
result = name[0].upper() + name[1:-1] + name[-1].upper()
print(result)
Output: SilambU
- First letter →
name[0].upper() - Last letter →
name[-1].upper()
Top comments (0)