In Python, a string is a sequence of characters. They are enclosed in single or double quotes. For example:
string1 = "Hello, World!"
string2 = 'Hello, World!'
Both of the above examples create strings with the same value.
You can use the + operator to concatenate strings, and the * operator to repeat a string a certain number of times. For example:
string3 = "Hello, " + "World!"
string4 = "Hello, " * 3
There are many useful built-in methods for strings in Python, such as find()
, replace()
, split()
, strip()
, join()
, etc.
For example:
string5 = "Hello, World!"
print(string5.find("World"))
print(string5.replace("World", "Universe"))
You can also use indexing and slicing to access parts of a string. For example:
string6 = "Hello, World!" print(string6[0]) print(string6[7:12])
You can also use len()
function to get the length of a string.
Additionally, strings are immutable, meaning that once a string is created, its value cannot be changed.
You can use f-strings(from python 3.6) to embed expressions inside string literals, using {}. For example:
name = "John" age = 30 print(f'{name} is {age} years old.')
This is a brief overview of strings in Python. There's a lot more to learn, including string formatting and advanced techniques for working with strings.
Here is an overview of some techniques for working with strings in Python:
- Concatenation: You can use the + operator to concatenate two or more strings together. For example:
string1 = "Hello"
string2 = " World"
string3 = string1 + string2
print(string3) # Output: "Hello World"
- Repetition: You can use the * operator to repeat a string a certain number of times. For example:
string4 = "Hello"
string5 = string4 * 3
print(string5) # Output: "HelloHelloHello"
- Formatting: You can use the
format()
method to insert values into a string. For example:
name = "John"
age = 30
string6 = "My name is {} and I am {} years old.".format(name, age)
print(string6) # Output: "My name is John and I am 30 years old."
- String Interpolation: f-strings(from python 3.6) are a way to embed expressions inside string literals, using {}. For example:
name = "John"
age = 30
string7 = f'My name is {name} and I am {age} years old.'
print(string7) # Output: "My name is John and I am 30 years old."
- Indexing and Slicing: You can use indexing to access individual characters in a string and slicing to access a range of characters. For example:
string8 = "Hello, World!"
print(string8[0]) # Output: "H"
print(string8[7:12]) # Output: "World"
- Built-in String Methods: Python has many built-in methods for working with strings, such as
find()
,replace()
,split()
,strip()
,join()
, etc. For example:
string9 = "Hello, World!"
print(string9.find("World")) # Output: 7
print(string9.replace("World", "Universe")) # Output: "Hello, Universe!"
- String Comparison: You can use comparison operators(<,>,<=,>=,==,!=) to compare strings. For example:
string10 = "Hello"
string11 = "World"
print(string10 == string11) # Output: False
- String Membership: You can use the
in
keyword to check if a string is present in another string. For example:
string12 = "Hello, World!"
print("World" in string12) # Output: True
- String Escaping: You can use the backslash() to escape special characters in a string. For example:
string13 = "Hello, \"World\"!"
print(string13) # Output: "Hello, "World"!"
- String Encoding/Decoding: You can use
encode()
anddecode()
methods to convert strings to different formats. For example:
string14 = "Hello, World!"
string15 = string14.encode()
print(string15) # Output: b'Hello, World!'
string
No comments:
Post a Comment