string¶
- Strings in Python are fundamental data structures used to represent sequences of characters.
- They are versatile and can store text, numbers (as text), symbols, and more.
- Single Quotes or Double Quotes
- You can enclose characters within single (
'
) or double ("
) quotes to create a string. - Both methods are equivalent.
- You can enclose characters within single (
In [ ]:
Copied!
my_string_1 = 'Hello, 🌏!'
my_string_2 = "This is also a string."
string_with_quotes = 'He said, "🙏!"'
my_string_1 = 'Hello, 🌏!'
my_string_2 = "This is also a string."
string_with_quotes = 'He said, "🙏!"'
- Triple Quotes (Multi-Line Strings)
- Use triple single (
'''
) or triple double ("""
) quotes for multi-line strings or strings containing quotes themselves.
- Use triple single (
In [ ]:
Copied!
multiline_text_1 = """This is a string
that spans multiple lines."""
multiline_text_2 = ''''This is also string
that spans multiple lines.'''
multiline_text_1 = """This is a string
that spans multiple lines."""
multiline_text_2 = ''''This is also string
that spans multiple lines.'''
- Accessing Characters using
Indexing
- Strings are like sequences (lists) of characters, where each character has an index starting from 0.
- You can access individual characters using their index within square brackets
[]
.
In [ ]:
Copied!
name = "Chaitanya"
first_letter = name[0] # 'C'
last_letter = name[-1] # 'a'
name = "Chaitanya"
first_letter = name[0] # 'C'
last_letter = name[-1] # 'a'
- Accessing Characters using
Slicing
- You can extract substrings using slicing, which involves specifying a start index (inclusive) and an end index (exclusive) within square brackets
[]
.
- You can extract substrings using slicing, which involves specifying a start index (inclusive) and an end index (exclusive) within square brackets
In [ ]:
Copied!
greeting = "Hello, 🌏!"
substring = greeting[7:9] # '🌏!'
greeting = "Hello, 🌏!"
substring = greeting[7:9] # '🌏!'
- String Concatenation
- The
+
operator combines strings to create a new string.
- The
In [ ]:
Copied!
first_name = "Chaitanya"
last_name = "Reddy"
full_name = first_name + " " + last_name
print(full_name) # Output: Chaitanya Reddy
first_name = "Chaitanya"
last_name = "Reddy"
full_name = first_name + " " + last_name
print(full_name) # Output: Chaitanya Reddy
- String Formatting with
f-strings
(Python 3.6+)- A concise and powerful way to format strings using f-strings, which embed variables or expressions directly within curly braces
{}
inside the string.
- A concise and powerful way to format strings using f-strings, which embed variables or expressions directly within curly braces
In [ ]:
Copied!
name = "Chaitanya"
age = 30
message = f"Hi {name}, you are {age} years old."
print(message) # Output: Hi Chaitanya, you are 30 years old.
name = "Chaitanya"
age = 30
message = f"Hi {name}, you are {age} years old."
print(message) # Output: Hi Chaitanya, you are 30 years old.
- String Formatting with
.format()
method (all Python versions)- The
format()
method allows string formatting with placeholders ({}
) replaced by arguments.
- The
In [ ]:
Copied!
name = "Chaitanya"
greeting = "Welcome, {}!".format(name)
print(greeting) # Output: Welcome, Chaitanya!
name = "Chaitanya"
greeting = "Welcome, {}!".format(name)
print(greeting) # Output: Welcome, Chaitanya!
- Common String Methods
len(string)
: Returns the length (number of characters) in the string.upper()
: Converts all characters to uppercase.lower()
: Converts all characters to lowercase.strip()
: Removes leading and trailing whitespaces.find(substring)
: Returns the index of the first occurrence of a substring, or -1 if not found.replace(old, new)
: Replaces all occurrences of a substring with another substring.
In [ ]:
Copied!
line = ' Hi Chaitanya, you are 30 years old'
print(len(line))
print(line.upper())
print(line.lower())
print(line.strip())
print(line.find('30'))
print(line.replace('Chaitanya', 'Chaitu'))
line = ' Hi Chaitanya, you are 30 years old'
print(len(line))
print(line.upper())
print(line.lower())
print(line.strip())
print(line.find('30'))
print(line.replace('Chaitanya', 'Chaitu'))