Python 3 – Strings
In Python, a string is a sequence of Unicode characters. We use single quotes or double quotes to denote a string.
Creating a String
We can create a string in either of the following two ways:
Single-line Strings
# Using Single quotes
string1 = 'This is an amazing string!'
# Using Double quotes
string2 = "This one is also pretty cool!"
Multi-line Strings
We can create multi-line strings in Python in two ways:
Using triple quotes
multiline_string = """This string has
multiple lines.
I am on a new line."""
Using escape characters
multiline_string = "This string also has \nmultiple lines. \nI am on a new line."
Accessing Characters in a String
Each element in a string has an index, and we use that index to access individual characters. The index starts from 0 for the first character in the string.
my_string = "Hello World!"
print(my_string[0]) # Output: H
String Slicing
In Python, we can slice a substring from a string by specifying both start index and end index. The slicing will return a new string with the characters in the specified range.
my_string = "Hello World!"
print(my_string[0:5]) # Output: Hello
If we don’t specify the start index, it takes 0 by default, and if we don’t specify the end index, it takes the end of the string by default.
my_string = "Hello World!"
print(my_string[:5]) # Output: Hello
print(my_string[6:]) # Output: World!
Negative indexing is also possible in Python, which means we can index a string from its end by using negative numbers.
my_string = "Hello World!"
print(my_string[-1]) # Output: !
print(my_string[-5:-1]) # Output: orld
String Methods
Python provides a lot of built-in methods for manipulating strings:
upper()
This method converts all the characters in a string to upper case.
my_string = "Hello World!"
print(my_string.upper()) # Output: HELLO WORLD!
lower()
This method converts all the characters in a string to lower case.
my_string = "Hello World!"
print(my_string.lower()) # Output: hello world!
replace()
This method replaces a part of a string with a new string.
my_string = "Hello World!"
print(my_string.replace("World", "Universe")) # Output: Hello Universe!
split()
This method splits a string into a list of substrings.
my_string = "Hello, World!"
print(my_string.split(",")) # Output: ['Hello', ' World!']
String Concatenation
In Python, we can concatenate two or more strings together to form a new string using the +
operator.
first_string = "Hello"
second_string = "World!"
result = first_string + " " + second_string
print(result) # Output: Hello World!
String Formatting
Python provides several methods to format strings. We can use placeholders, format() method or f-string to format strings.
Placeholders
name = "John"
age = 34
print("My name is %s, and I am %d years old." % (name, age))
format() Method
name = "John"
age = 34
print("My name is {}, and I am {} years old.".format(name, age))
f-String
name = "John"
age = 34
print(f"My name is {name}, and I am {age} years old.")
Conclusion
In this article, we learned about strings in Python. We learned how to create strings, access characters in a string, slice strings, use string methods, concatenate strings, and format strings. Strings are an essential part of any programming language, and mastering them is crucial to becoming proficient in Python.