Table of Content
Strings
In python, anything that you enclose between single or double quotation marks is considered a string. A string is essentially a sequence or array of textual data. Strings are used when working with Unicode characters.
Let us understand with an example.
Simple string:
python
my_string = "Hello, World!"
print(my_string)
Output:
python
Hello, World!
String with single quotes:
python
my_string = "Hello, World!"
print(my_string)
Output:
python
Hello, World!
Do you know we can write multiline string using triple quotes?
python
multiline_string = """This is a multiline string.
It spans across multiple lines.
"""
print(multiline_string)
Output:
python
Hello, World!
String with formatting:
python
name = "Alice"
age = 30
formatted_string = f"Hello, my name is {name} and I am {age} years old."
print(formatted_string)
Output:
python
Hello, my name is Alice and I am 30 years old.
Accessing individual characters of a string:
python
my_string = "Python"
first_character = my_string[0]
print(first_character)
Output:
python
'P'
String concatenation
python
first_name = "John"
last_name = "Doe"
full_name = first_name + " " + last_name
print(full_name)
Output:
python
John Doe