A string is a sequence of characters, where each character can be an alphabet, a number, a special character, etc. Python uses Unicode to represent characters, which means it can handle a vast array of symbols and alphabets from various languages.
While ASCII encodes a limited set of characters, Unicode provides a unique value for every possible character in every language. With modern applications requiring support for diverse symbols and languages, Unicode is the superior choice.
You can assign strings using single, double, or triple quotes.
# Assigning using single quotes
string1 = 'Welcome'
# Assigning using double quotes
string2 = "to"
# Assigning using triple quotes
string3 = '''FACE Prep's Python Programming Tutorial'''
print(string1)
print(string2)
print(string3)
Welcome
to
FACE Prep's Python Programming Tutorial
When a string contains quotes, use the opposite type of quotes or escape sequences.
# Using double quotes for a string containing single quotes
string1 = "Welcome to 'Python' course"
print(string1)
Welcome to 'Python' course
For multiline strings, use triple quotes:
string1 = """Welcome
to
FACE Prep's Python Programming Tutorial"""
print(string1)
Welcome
to
FACE Prep's Python Programming Tutorial
Access single characters using indices.
string1 = "FACE Prep's Python Tutorials"
print(string1[1]) # Prints 'A'
print(string1[-1]) # Prints 's'
A
s
Use slicing syntax string[start:end]
to access parts of a string.
print(string1[12:18]) # Prints 'Python'
print(string1[-12:-2]) # Prints 'Tutorial'
Python
Tutorial
Strings in Python are immutable. You cannot modify them directly but can reassign a new value.
string1 = "FACE Prep"
string1 = "New FACE Prep"
print(string1)
# Deleting a string
string2 = "Python Tutorials"
del string2
# print(string2) # Raises NameError
New FACE Prep
Traceback (most recent call last):
NameError: name 'string2' is not defined
Escape sequences start with a backslash () to handle special cases like quotes or newlines.
string1 = 'I\'m in love with Python Language'
print(string1)
I'm in love with Python Language
Raw strings (r
or R
) ignore escape sequences:
string1 = R'I prefer learning Python\Java'
print(string1)
I prefer learning Python\Java
a = "Hello"
b = "World"
print(a + " " + b)
print(a * 3) # Prints 'HelloHelloHello'
print("World" in b) # True
print("Python" not in b) # True
Use the format()
method for inserting variables into strings.
a = 5
b = "My age is {}"
print(b.format(a))
For multiple values:
c = "My name is {0} and my age is {1}."
print(c.format("John", 30))
My name is John and my age is 30.
Python provides various built-in methods for string manipulation. Here are a few:
Method | Description |
---|---|
upper() | Converts string to uppercase |
lower() | Converts string to lowercase |
strip() | Removes leading/trailing spaces |
replace(a, b) | Replaces a with b |
split() | Splits string into a list |
join() | Joins elements of a list into a string |
string = " Python Programming "
print(string.upper())
print(string.strip())
print(string.replace("Programming", "Language"))
PYTHON PROGRAMMING
Python Programming
Python Language
Strings in Python are an essential data type used to store and manipulate textual data. They are immutable, meaning their values cannot be changed after creation, which ensures data integrity. Python provides various methods and operations to work with strings, making it easy to perform tasks such as concatenation, slicing, formatting, and searching. Understanding how to handle strings effectively will enable you to process and manipulate text in a wide range of applications, from basic data handling to complex text processing tasks.
Click here to know more our program!