A String in Python is a collection of characters (letters, numbers, symbols, or spaces) enclosed within quotes.
We can use single quotes (' '), double quotes (" "), or triple quotes (''' ''').
- Example of String:
name = "Sathish"
city = 'Chennai'
message = "Hello World"
print(name)
print(city)
print(message)
Output:
Sathish
Chennai
Hello World
- Types of String Quotes:
Single quotesname = 'Sathish'# Double quotescity = "Chennai"# Triple quotesmessage = """Welcome to Python"""
String Operations:
Concatenation (Joining strings):
first = "Hello"
second = "World"
print(first + " " + second)
Output:
Hello World
- Repetition:
name = "Hi"
print(name * 3)
Output:
Hi Hi Hi
- Length: name = "Sathish" print(len(name))
Output:
7
- Accessing characters (Indexing):
name = "Python"
print(name[0])
print(name[1])
Output:
P
y
- Important String Methods: Method Purpose upper() Converts to uppercase lower() Converts to lowercase capitalize() Capitalizes first character replace() Replaces characters strip() Removes leading/trailing spaces split() Splits a string into a list find() Finds a substring
Example:
name = "sathish"
print(name.upper())
print(name.capitalize())
Output:
SATHISH
Sathish
Top comments (0)