In Python, a string is a sequence of characters. Strings can be enclosed in single quotes (‘), double quotes (“), or triple quotes (”’ or “””).
Here are a few examples of strings in Python:
# Single-quoted string
string1 = 'Hello, world!'
# Double-quoted string
string2 = "Hello, world!"
# Triple-quoted string
string3 = """Hello, world!"""
You can use the + operator to concatenate strings, and the * operator to repeat a string a given number of times. For example:
name = "John"
greeting = "Hello, " + name + "!"
print(greeting) # Output: "Hello, John!"
# Repeat the string 10 times
print("=" * 10) # Output: "=========="
You can also use string formatting to insert values into a string. For example:
name = "John"
age = 30
message = "Hello, my name is {} and I am {} years old.".format(name, age)
print(message) # Output: "Hello, my name is John and I am 30 years old."
Python has a number of built-in methods for manipulating strings. Here are a few examples:
str.upper(): Returns a copy of the string with all letters converted to uppercase
str.lower(): Returns a copy of the string with all letters converted to lowercase
str.strip(): Returns a copy of the string with leading and trailing whitespace removed
str.split(sep): Returns a list of substrings split at the specified separator sep
Here are a few more things you might want to know about strings in Python:
- String indexing: You can access individual characters in a string using indexing. In Python, indexing starts at 0, so the first character in a string is at index 0, the second character is at index 1, and so on. You can use negative indexing to access characters from the end of the string. For example:
string = "Hello, world!"
# Access the first character (index 0)
print(string[0]) # Output: "H"
# Access the last character (index -1)
print(string[-1]) # Output: "!"
# Access the second-to-last character (index -2)
print(string[-2]) # Output: "d"
- String slicing: You can use slicing to extract a sub-string from a string. Slicing works by specifying a start index and an end index separated by a colon (:). The start index is inclusive and the end index is exclusive. For example:
string = "Hello, world!"
# Extract a sub-string from index 1 to index 5 (exclusive)
print(string[1:5]) # Output: "ello"
# Extract a sub-string from index 0 to index 5 (exclusive)
print(string[:5]) # Output: "Hello"
# Extract a sub-string from index 6 to the end of the string
print(string[6:]) # Output: "world!"
# Extract a sub-string from the beginning of the string to the end
print(string[:]) # Output: "Hello, world!"
- String immutability: In Python, strings are immutable, which means that once you create a string, you cannot change it. If you want to modify a string, you have to create a new string with the desired modifications. For example:
string = "Hello, world!"
# This will cause an error, because strings are immutable
string[0] = "h"
# Instead, you can create a new string with the desired modifications
string = "hello, world!"
For more information please visit python.org
This article is created based on experience but If you discover any corrections or enhancements, please write a comment in the comment section or email us at contribute@devopsforu.com. You can also reach out to us from Contact-Us Page.
Follow us on LinkedIn for updates!