In computer programming, a string is a sequence of characters. For example, “hello” is a string that contains a string of characters
‘h’, ‘e’, ‘l’, ‘l’ and ‘o’.
We use single quotes or double quotes to represent a string in Python. For example
, # create a string using double quotes string1 = “Python programming” # create a string using single quotes string1 = ‘Python programming’
Here, we have created a string variable called string1. The variable is initialized with the Python Programming string.
Example: Python String
# create string type variables name = “Python” print(name) message = “I love Python.” print(message)
Output
Python I love Python.
In the previous example, we have created variables of type string: name and message with the values “Python” and “I love Python” respectively.
Here, we
have used double quotes to represent strings, but we can also use single quotation marks.
Accessing string characters in
Python
We can access the characters in a string in three ways
.
- Indexing: One way is to treat strings as a list and use index values. For example
, greet = ‘hello’ # access 1st index element print(greet[1]) # “e” Negative indexing
- : Similar to a list, Python allows negative indexing for your strings. For example,
greet = ‘hello’ # access 4th last element print(greet[-4]) # “e”
- Segmentation: Access a range of characters in a string using the colon cut operator:. For example
, greet = ‘Hello’ # access character from the 1st index to the 3rd index print(greet[1:4]) # “ell”
Note: If we try to access an index outside the range or use numbers that are not an integer, we will get errors
. Python strings are
immutable
In Python, strings are immutable. That means that the characters in a string cannot be changed. For example
, message = ‘Hello Friends’ message[0] = ‘H’ print(message)
Output
TypeError: ‘str’ object does not support element mapping
However, we can assign the variable name to a new string. For example,
message = ‘Hello Friends’ # assign new string to message variable message = ‘Hello Friends’ prints(message); #prints “Hello Friends” Python
Multiline String
We can also create a multiline string in Python. For this, we use triple double quotation marks “”” or triple single quotation marks ”’. For
example, # multiline string message = “”” It will never give you up It will never disappoint you “””
(message) Output It will never give you up It will never disappoint
you In the example above, anything inside the triple quotation marks that include it is a multiline string
. Python
string
operations
There are many operations that can be performed with strings, making it one of the most commonly used data types in Python
.
1. Compare
two strings
We use the == operator to compare two strings. If two strings are equal, the operator returns True. Otherwise, it returns False. For
example, str1 = “Hello, world!” str2 = “I love Python.” str3 = “Hello, world!” # compare str1 and str2 print(str1 == str2) # compare str1 and str3 print(str1 == str3)
Output
False True
In the example above,
- str1 and str2 are not equal. Therefore, the result is False.
- STR1 and STR3 are the same. Therefore, the result is True.
Arabic numeral. Join
two or more strings In Python, we can join (
concatenate) two or more strings
using the + operator. greet = “Hello, ” name = “Jack” # using + operator result = greet + name print(result) # Output: Hello, Jack
In the example above, we used the + operator to join two strings: greet and name
. Iterate through a
Python string We can iterate through
a
string using a for loop. For example
, greet = ‘Hello’ # iterating through greet string for letter in greet: print(letter)
Output
H e l l o Python
String Length
In Python, we use the len() method to find the length of a string. For example
, greet = ‘Hello’ # count length of greet string print(len(greet)) # Output: 5-string membership test
We can test whether a substring exists within a string or not, using the in keyword
. print(‘a’ in ‘program’) # True print(‘at’ not in ‘battle’) Fake Python string methods In addition to those mentioned above, there are several
string
methods
present in Python. Here are some of those methods:
Escape sequences
in Python
The escape sequence is used to escape some of the characters present within
a string.
Suppose we need to include
double quotes and single quotes inside a string, example = “He said, “What’s there?”” print(example) # throws error
Since strings are represented by single or double quotes, the compiler will treat “He said, ” as the string. Therefore, the above code will cause an error.
To solve this problem, we use the \ escape character in Python.
# escape double quotes example = “He said, \”What is there?\”” # escape single quotes example = ‘He said, “What is there?”‘ print(example) # Output: He said, “What is there?”
Here is a list of all escape sequences supported by
Python. Python String Format
(f-Strings)
Python f strings make it easy to print values and variables. For example
, name = ‘Cathy’ country = ‘UK’ print(f'{name} is from {country}’)
Output
Cathy is from the United Kingdom Here, f'{name} is from
{country}’ is a string f
.
This new formatting syntax is powerful and easy to use. From now on, we will use f-Strings to print strings and variables.