Strings in Python are one of the most essential data types that are used for storing and working with text. In fact, every sequence of characters such as letters, numbers, and symbols that falls under the indicators of a string (single or double quotes) is recognized as a string. For example, "hello" or '123' are examples of strings in Python.
One of the main advantages of working with strings in Python is their simplicity and flexibility. You can easily perform many operations such as concatenation, partitioning, repeating, and even searching on strings. Additionally, Python provides a collection of functions and methods (built-ins) for working with strings that can be utilized for carrying out more complex operations.
To work with strings, you must first define them. In Python, you can define strings using either single or double quotes, and there is no difference in how they are defined. However, for longer strings that exceed one line, you can use three quotes.
To define a string, you can use single or double quotes:
string_1 = "hello world!"
string_2 = 'Python is powerful.'
multiline_string = '''This is an example of
multiple line string in Python.'''
concatenated = string_1 + " " + string_2
print("Length of string: ", len(string_1))
print("Concatenated string: ", concatenated)
First of all, we define two strings with both single and double quotes.
string_1 = "hello world!"
string_2 = 'Python is powerful.'
Then, we define a multi-line string using three quotes.
multiline_string = '''This is an example of\nmultiple line string in Python.'''
To concatenate two strings, we use the operator +
.
concatenated = string_1 + " " + string_2
Using the len()
function, we can get the length of a string.
print("Length of string: ", len(string_1))
Finally, we can display the result using the print()
function.
print("Concatenated string: ", concatenated)