A string is a sequence of characters that represent text-based information. A character is a single unit of data that represents a symbol, letter, digit, or any other symbol from a character set, such as Unicode. Examples of  characters include the alphanumeric characters (a-z, A-Z, 0-9)  and the special characters such as #, &, !, ^, %, @ , etc.

A string is  represented in the memory  simply as an array of characters terminated with a special character known as the null character. In some languages such as C and C++, it is possible to interact with strings at extremely low level. Python, on the other hand, abstracts the strings in a way that a programmer does not necessarily need to care about how they are stored and represented in the memory.

Strings are the most commonly  used data types in Python. Some common use cases of strings are:

  1. Storing and manipulating textual data, such as user input, file contents, and database records.
  2. Formatting output to display data in a readable and organized manner, such as with print statements or logging messages.
  3. Processing and analyzing text data, such as with natural language processing or regular expressions.
  4. Developing and testing software, where strings can be used for debugging, logging, and unit testing.

How are strings represented in Python?

In Python, a string is anything that is enclosed in single quotes(' ') , double quotes(" ") or triple single or double quotes(''' ''', """ """).

Examples:

'Hello, World!'
"Hello, World!"
'''Hello, World!'''
"""Hello, World!"""

The simplest string in Python is simply a single or double quote without anything inside them('', ""), this represents an empty string.

Python does not differentiate between single or double quotes when defining a string, this means that 'Hello, World!' and "Hello, World!" are exactly the same thing. However, you cannot mix the two formats, for example 'Hello, World!" will result in SyntaxError, if you use one format for opening you must as well use it as the closing format.

ExampleEdit & Run
'Hello, World!" #SyntaxError
Output:
  File "<string>", line 1    'Hello, World!" #SyntaxError    ^SyntaxError: unterminated string literal (detected at line 6)[Finished in 0.01081387186422944s]

The single and double quotes are the most commonly used string formats, the triple quotes are in most cases, used only when a string spans multiple lines. 

'''This is an example
of a string
that spans multiple lines'''

In some languages such as C and C++, characters and strings are two distinct data types but in Python there is no data type to represent single characters,  a character is simply a string made of just one character.  Examples of characters in Python:

'a'
"b"
'4'
"7"
'@'

Escaping special characters

Consider if we want to use a string which contains an apostrophe e.g john's, in this case, if we use single quotes like 'john's'  , the string will be closed prematurely leading to a  SyntaxError. 

ExampleEdit & Run
'Johns's' #SyntaxError
Output:
  File "<string>", line 1    'Johns's' #SyntaxError            ^SyntaxError: unterminated string literal (detected at line 6)[Finished in 0.010130219161510468s]

To solve this issue, we can use the double quotes format. If a string contains double quotes  we can as well use the single quotes to avoid premature termination of the string.

ExampleEdit & Run
print( "John's" )

print( '"Hello, World!"' )
Output:
John's"Hello, World!"[Finished in 0.009519632207229733s]

In cases where we want to use both single and double quotes inside a string, we can use  the  triple quotes  formats as follows:

ExampleEdit & Run
my_string = '''She said, "Don't forget to bring the umbrella".'''

print( my_string )
Output:
She said, "Don't forget to bring the umbrella".[Finished in 0.009787668939679861s]

The backlash (\) , also known as the escape character,  is used to allow special characters in a string. Special characters are characters that have a special purpose such as  the backlash, single quote, double quotes, newline character e.t.c. 

For example  to use the single quotes format for  the string John's, we can do it as follows:

ExampleEdit & Run
print( 'John\'s' )
Output:
John's[Finished in 0.009702356066554785s]

our other examples above using the backlash character:

ExampleEdit & Run
print( "\"Hello, World!\"" )

my_string = "She said, \"Don\'t forget to bring the umbrella\"."
print( my_string )
Output:
"Hello, World!"She said, "Don't forget to bring the umbrella".[Finished in 0.009499122155830264s]

The backslash character before the single or double quotes tells Python to treat them as literal characters within the string.

We can add a new line  in a string by using the backlash followed by letter  ( \n ) :

ExampleEdit & Run
print( "Denver\nNairobi\nBogota\nManilla\nTokyo" )
Output:
DenverNairobiBogotaManillaTokyo[Finished in 0.010163198923692107s]

The backslash must itself be escaped to occur as a natural character of the string literal, as in:

ExampleEdit & Run
print("C:\\Python\\")
Output:
C:\Python\[Finished in 0.011271726107224822s]

The following list shows some escape  characters and their purpose

Escape Character  Meaning
\\ Backlash
\' Single quote
\" Double quote
\a Alert
\b Backspace
\f Formfeed
\n Newline
\r Carriage return
\t Horizontal tab
\v Vertical tab
\0 Null

Raw Strings

Raw strings are used to create string literals that ignore all escape characters. To create a raw string, we add the letter "r" before the first quote character of the string.

ExampleEdit & Run
print(r'Denver\nTokyo')

print(r'John\'s')
Output:
Denver\nTokyoJohn\'s[Finished in 0.01063112705014646s]

As you can see, the "\n" character in r"Denver\nTokyo" is not interpreted as a newline character in the output, but instead it is treated as two separate characters (a backslash and the letter "n").

Consider a string  to represent a path on Windows directories, without using a raw string, we will have to use the double backslashes  ( \\ ): For example to open a file .

open('C:\\users\\user\\desktop\\text.dat', 'w')

If we use a raw string, one backslash will be okay as shown below:

open(r'C:\users\user\desktop\text.dat', 'w')

In raw strings all the escape characters are treated just as normal characters. However, a string whether raw or not cannot end with a single backlash or to be more precise, an odd number of backlashes. This is because the trailing backlash will escape the closing quote character. 

ExampleEdit & Run
print( r"Hello\" ) #SyntaxError
Output:
  File "<string>", line 1    print( r"Hello\" ) #SyntaxError           ^SyntaxError: unterminated string literal (detected at line 6)[Finished in 0.010052759200334549s]

In  this case, two backslashes still can't be used like we would do in a normal( non raw ) string because both of them will be included: 

ExampleEdit & Run
print( r"Hello\\" )
Output:
Hello\\[Finished in 0.009806971997022629s]

What we can do if a raw string ends with a backlash is use two backlashes, then slice off the last one as follows:

ExampleEdit & Run
my_string = r"Hello\\"[0:-1]

print( my_string )
Output:
Hello\[Finished in 0.00978145096451044s]

Or just use a normal string with the escape character:

ExampleEdit & Run
my_string = "Hello\\"

print(my_string)
Output:
Hello\[Finished in 0.009842352010309696s]

Operations on Strings

We can perform operations on strings in order to  manipulate them or extract information from them. Some common operations on strings are:

String Length

The length of a string is the number of  characters that makes it, including white spaces and special characters. The builtin len() function is used to get the length of a given string.

ExampleEdit & Run
print( len("a") )

print( len("abc") )

print( len("Hello, World!") )

print( len("Welcome to Pynerds!") )
Output:
131319[Finished in 0.009342903969809413s]

An empty string has a length of 0

ExampleEdit & Run
print( len("") )
Output:
0[Finished in 0.00912663503549993s]

String concatenation and Repetition

Concatenation means creating a new string by joining/adding  two or more separate strings , in Python this operation is achieved using  the  "+" operator. 

ExampleEdit & Run
print( "Hello, " + "World!" )

print( "John" + " Doe" )

print( "String " + "Concatenation " + "in " + "Python" )
Output:
Hello, World!John DoeString Concatenation in Python[Finished in 0.010495260125026107s]

Both of the operand in string concatenation must be strings, for example using an integer as one of the operand will raise a TypeError error.

ExampleEdit & Run
"3" + 3  #TypeError: 
Output:
Traceback (most recent call last):  File "<string>", line 1, in <module>TypeError: can only concatenate str (not "int") to str[Finished in 0.010770180029794574s]

The  "*" operator is used for string repetition. The operator takes two operands,  the string to repeat and an integer for the number of times the string will be repeated. 

ExampleEdit & Run
print( "Hello" * 3 )

print( "Hello " * 3 )

print( "33" * 3 )
Output:
HelloHelloHelloHello Hello Hello 333333[Finished in 0.010126669192686677s]

If both operations are used in the same expression, repetition is done before concatenation.

ExampleEdit & Run
print( "5" + "4" * 3 )

print( "5" + "4" * 3 + "3" + "2" * 3 )
Output:
544454443222[Finished in 0.010095366975292563s]

Indexing and Slicing 

A string is a collection of characters,  we can access individual characters in a given string by use of Indexing.  Each character in a string have an integer index which defines its position in the string. The first character in a string has an index 0, the second character has an index 1 , the third character has an index 2 , and so forth.  For example for in the string "Hello, World!", the characters will be positioned as shown below:

How characters are arranged in a Python string

If you are new to programming you should avoid the mistake of assuming that the indices starts at  1.  Even in all the other sequence-based data types such as lists and tuples,  the first index will always be  0, and this is still the case in most other programming languages. 

To get the character at a given position/index we use the [] operator with the following syntax:

<string>[<index>]
ExampleEdit & Run
S = "Hello, World!"

print( S[0] )

print( S[1] )

print( S[4] )

print( S[12] )
Output:
Heo![Finished in 0.00964809488505125s]

Since the first character has an index of 0, the last character in any string will always have an index which is the total  number of characters minus 1,  or simply the string's length minus 1. For example, the string "Hello, World!" has a length of  13 , therefore, the last character which is ( ! ) has an index 12 .

ExampleEdit & Run
my_string = "Hello, World!"

print( my_string[len(my_string) - 1] )
Output:
![Finished in 0.0097860770765692s]

Negative Indexing

Python allows indexing from back to front. Negative indices are used in this approach where the last character in the string has an index -1 , the second last has an index -2 , the third last has an index  -3, and so forth, until we reach the first element which has an index which is negative the length of  the string. 

ExampleEdit & Run
S = "Hello, World!"

print( S[-1] )

print( S[-2] )

print( S[-13] )

print( S[-12] )
Output:
!dHe[Finished in 0.009279853897169232s]

From the two indexing approaches i.e using positive or negative indices, we get that the valid indices which represents  characters in a string, starts at negative the length of the string and ends at the length of the string minus . For example in the string "Hello, World!" which has a length of13, the valid index  values are  -13 to 12 . Trying to Use  an index which is not within this range will raise an IndexError

ExampleEdit & Run
S = "Hello, World"

S[13] #IndexError

S[-16] #IndexError
Output:
Traceback (most recent call last):  File "<string>", line 3, in <module>IndexError: string index out of range[Finished in 0.009766543051227927s]

Slicing

While indexing is used to get the character at a specific index, Slicing can be used to get a substring containing  characters in a given range of indices. The [] operator is still used in slicing but with a different syntax in order to capture a range. The syntax is as shown below:

<string>[start : stop : step]

The start indicates the index at which the range will begin, while the stop indicates the index where the range will end, the stop itself is not included in the range. For example,  to get the first 5 characters,  the value of start  will be 0, and the value of stop will be 5 , these are the values in the indices 0,1,2,3,4 

The step is optional , if it is not included , will be used as the default value. The value given as step is used as the jump value in the range, for example:

ExampleEdit & Run
S = "Hello, World!"

print( S[0 : 10 : 1] )
 
print( S[0 : 10 : 2] )
Output:
Hello, WorHlo o[Finished in 0.009497948922216892s]

Without step, the syntax is: 

<string>[start : stop]
ExampleEdit & Run
S = "Hello, World!"

print( S[0 : 5] )

print( S[0 : 10] )

print( S[7 : 12] )
Output:
HelloHello, WorWorld[Finished in 0.010606496129184961s]

In slicing, unlike in indexing, values outside the range can be used without raising an IndexError . If the value of start, is smaller than the smallest valid index, the slicing will begin at the first character and if the value of stop is larger than the  largest valid index, the slicing will end at the last character in the string.

ExampleEdit & Run
S = "Hello, World!"

print( S[5 : 100] )

print( S[-50 : 8])

print( S[-50 : 100] )
Output:
, World!Hello, WHello, World![Finished in 0.010956078069284558s]

As shown above, we can mix negative and positive indices as the values of start  or stop . For example to get all the characters from the fifth indices up to the second last , we can use 5  as the start and -1 as the stop. 

ExampleEdit & Run
S = "Hello, World!"

print( S[5 : -1] )

print( S[7 : -1] )

print( S[0 : -8] )

print( S[-6 : -1] )
Output:
, WorldWorldHelloWorld[Finished in 0.010124973952770233s]

Using the value of start larger than the value of stop will result in an empty range and an empty string will be returned.

ExampleEdit & Run
S = "Hello, World!"

print( S[10 : 0] )
Output:
[Finished in 0.00987267098389566s]

The Short Hand Slicing

Consider if we want to get the characters from a certain index up to the last character in the string. In this case, we can use the string's length as the stop value, or just any other number larger than the strings length. For example:

ExampleEdit & Run
S = "Hello, World!"

print( S[7 : len(S)] )

print( S[0 : len(S)] )

print( S[5 : len(S)] )
Output:
World!Hello, World!, World![Finished in 0.009605339029803872s]

The above examples in short hand slicing will be as follows:

ExampleEdit & Run
S = "Hello, World!"

print( S[7 :] )

print( S[0 :] )

print( S[5 : ] )
Output:
World!Hello, World!, World![Finished in 0.009294997900724411s]

We simply ignore the stop  and the slicing goes on up to the last index.

We can also ignore the start and the slicing will begin from index 0.

ExampleEdit & Run
S = "Hello, World!"

print( S[ : 5] )

print( S[ : 10] )

print( S[ : 8] )
Output:
HelloHello, WorHello, W[Finished in 0.009262093808501959s]

Ignoring both start and stop will result in exactly the same string:

ExampleEdit & Run
S = "Hello, World!"

print( S[ : ] )
Output:
Hello, World![Finished in 0.009572192095220089s]

If the  step need to be included in the short hand syntax, two colons must be used.

ExampleEdit & Run
S = "Hello, World!"

print( S[ 5 :: 2] )

print( S[ : 10 : 2 ] )
Output:
,WrdHlo o[Finished in 0.009875366929918528s]

Reversing a String 

The short hand slicing with no start and stop  and with -1 as the step can be used to easily reverse a string.

ExampleEdit & Run
S = "Hello, World!"

print( S[ :: -1 ] )
Output:
!dlroW ,olleH[Finished in 0.009897602954879403s]