Python Tutorial
Python Variable
Python Operators
Python Sequence
Python String
Python Flow Control
Python Functions
Python Class and Object
Python Class Members (properties and methods)
Python Exception Handling
Python Modules
Python File Operations (I/O)
In Python, strings are sequences of characters enclosed in single quotes (' '
) or double quotes (" "
). This tutorial will guide you through working with strings in Python, including long strings and raw strings.
Creating strings:
You can create strings using either single or double quotes:
# Creating strings string1 = 'Hello, World!' string2 = "Python is awesome!" print(string1) # Output: Hello, World! print(string2) # Output: Python is awesome!
To include quotes within a string, use the opposite type of quotes or escape the quote character with a backslash (\
):
# Using quotes within strings string3 = 'I\'m learning Python!' string4 = "He said, \"Python is amazing!\"" print(string3) # Output: I'm learning Python! print(string4) # Output: He said, "Python is amazing!"
Long strings (multiline strings):
To create multiline strings, use triple quotes (''' '''
or """ """
):
# Creating multiline strings multiline_string1 = '''This is a multiline string.''' multiline_string2 = """Another example of a multiline string.""" print(multiline_string1) print(multiline_string2)
Raw strings:
Raw strings in Python are prefixed with an 'r' or 'R' before the opening quote, and they treat backslashes as literal characters instead of escape characters. This is useful when working with file paths, regular expressions, or any string containing lots of backslashes:
# Creating raw strings raw_string1 = r'C:\Users\John\Desktop' raw_string2 = R'Regex pattern: \w+\d{4}' print(raw_string1) # Output: C:\Users\John\Desktop print(raw_string2) # Output: Regex pattern: \w+\d{4}
String concatenation:
You can concatenate strings using the +
operator:
first_name = "John" last_name = "Doe" full_name = first_name + " " + last_name print(full_name) # Output: John Doe
String formatting:
There are several ways to format strings in Python, such as using f-strings, the str.format()
method, or the %
operator:
# Using f-strings (Python 3.6+) name = "John" age = 30 f_string = f"My name is {name} and I am {age} years old." print(f_string) # Output: My name is John and I am 30 years old. # Using str.format() format_string = "My name is {} and I am {} years old.".format(name, age) print(format_string) # Output: My name is John and I am 30 years old. # Using the % operator percent_string = "My name is %s and I am %d years old." % (name, age) print(percent_string) # Output: My name is John and I am 30 years old.
In summary, Python provides various ways to work with strings, including long strings and raw strings. By understanding how to create, concatenate, and format strings, you can manipulate and display text effectively in your Python programs.
Creating and initializing strings in Python:
my_string = "Hello, Python!"
String literals and constants in Python:
None
for representing null or absent values.string_literal = "This is a string" null_value = None
Concatenating strings in Python:
+
operator or by using the join()
method.first_name = "John" last_name = "Doe" full_name = first_name + " " + last_name
String indexing and slicing in Python:
my_string = "Python" first_char = my_string[0] # 'P' substring = my_string[1:4] # 'yth'
Escape characters and special sequences in Python strings:
\n
, \t
) allow you to include special characters in strings.special_string = "This is a line\nAnd this is a new line"
Multiline strings in Python:
'''
or """
) allow you to create multiline strings.multiline_string = """ This is a multiline string in Python """
Raw strings in Python:
r
or R
) treat backslashes as literal characters, useful for regular expressions or file paths.raw_string = r"C:\Users\Username\Documents"
Common operations and methods for strings in Python:
my_string = "Hello, Python!" length = len(my_string) uppercase = my_string.upper()