In this part of the tutorial, we will explore strings in the Python programming language. You will have a good understanding of strings by the end of this tutorial.

Hello & Welcome!

Part 3- Python Tutorial: Strings in Python

We will explore the following topics in this tutorial:

  • How to Represent Strings
  • Storing Strings in Variables
  • Strings as Arrays
  • String Slicing Techniques
  • Commonly Used String Functions

How to represent Strings?

Either single quotation marks (‘ ‘) or double quotation marks (” “) are always used to represent strings in Python.

For Example-

print("hello")
print('hello')

OUTPUT-
hello
hello

But there is a problem if you need to write ‘He’s a good man’. It will result in a syntax error because the apostrophe in ‘He’ prematurely ends the string.

You can use double quotation marks (” “) for words with apostrophes to avoid this problem. Here’s an example:

print("He's a good man")
OUTPUT-
He's a good man

Another way to handle this problem is by using a backslash (\) before the apostrophe. For example: ‘he\’s a good man’.

->Let’s see in this example-

print('He\'s a good man')
OUTPUT-
He's a good man

Store String to Variable

We can assign a string to a variable in the same way we assign a number to a variable. Let’s take a look at how you can store a string in a variable:

>>>greet = 'Hello'
>>>print(greet)

OUTPUT-
Hello

String in Array Form

Strings are ordered sequences of bytes used to represent Unicode characters in Python. However, Python does not have a dedicated character data type, instead, a single character is simply a string of length one.

Similar to arrays, strings use square brackets [ ] to access individual elements.

If you need to retrieve a specific character from a string, you can easily do so by writing the following line:

>>>greet[0]

OUTPUT-
'H'

Here, we are taking greet = ’ Hello’.

In this example, the output is ‘H’ because we are accessing index number 0.

Note: Indexing always starts from 0 (zero). For example, the index of the first item is 0, the second item is 1, and continues sequentially.

By Using Negative Indexing

You can achieve the same result in reverse order by simply using a negative index like -1.

Confused?  Let’s see an example to clear your confusion.

>>>greet[-1]

OUTPUT-
'O'

It is returning ‘o’ because we are passing -1 as an index. In negative indexing:

-1 represents ‘o

-2 represents ‘l’

-3 represents ‘l’

-4 represents ‘e’

-5 represents ‘h’

Note: Backward indexing begins at -1 while forward indexing starts at 0. To better understand indexing, let’s examine the following example.

-5 -4 -3 -2 -1 #Backward Indexing
H e l l o
0 1 2 3 4 #Forward Indexing

String Slicing

Slicing is a technique for extracting specific portions from a whole. We can apply the same concept in Python for slicing a string.

You can retrieve only a certain section of a word by using the slicing technique. Are you ready to learn how to perform slicing?

Let’s take the same example: greet = ‘Hello’. You can extract a portion of the word “Hello” by using the following code:

>>> greet [0:3]

OUTPUT-
'Hel'

greet[0:3] indicates that the slicing begins at index 0 and goes up to 3, but does not include index 3. It retrieves elements from index 0 to 2. Let’s explore this in detail:

H e l l o
0 1 2 3 4
---- #slice till index 2

You can use negative indexing to perform slicing. Here’s how you can do it:

>>>greet [2:-1]

OUTPUT-
'll'

You might be wondering how it returns ’ll’ as the result. Don’t worry, I’ll explain how ‘ll’ appears in the output.

-5 -4 -3 -2 -1 
H e l l o
0 1 2 3 4
-------
  • greet[2: -1] starts from index 2, which corresponds to ‘l’, and goes up to the index before -1, which is index -2. At index -2, the character is also ‘l’, because of this it is returning the output ‘ll’.

The value of greet remains ‘Hello’ despite performing all these operations. You can permanently update the value of the variable by reassigning it.  Let’s see how this can be done.

>>>greet = greet[0:3]
>>>greet

OUTPUT-
'Hel'

Most Used String Functions

You can execute multiple operations using strings functions. Now we will explore some commonly used functions. Let’s take a look!

String Concatenation

Using the ‘+’ operator you can easily combine or join two strings. Let’s see in this example-

>>>greet = 'Hello'
>>>str = 'John'
>>>greet + str

OUTPUT-
'HelloJohn'

Notice there is no gap between “Hello” and “John”. Just add ‘ ‘ between them to insert a space. See in the example below-

>>>greet+' '+str

OUTPUT-
'Hello John'

Repetition of Strings

Use the * symbol followed by the desired number of repetitions to repeat a word multiple times. Let’s see in the example below-

>>>greet*3

OUTPUT-
'HelloHelloHello'

Note: Simply use the dot (.) operator to apply any string method.

String UPPER CASE

To convert a string from lowercase to uppercase you can use the Upper () method. Let’s see the example.

>>>greet.upper()

OUTPUT-
'HELLO'
  • Simply use the lower () method to convert uppercase letters to lowercase.

String Splitting

To split a string into different parts you can use the split (‘,’) method. Let’s see in the example below-

>>>Names= "John, mike, alex"
>>>Names.split(',')

OUTPUT-
['John','mike','alex']

String Length

Use the len(string) function to determine the length of a string. For example

>>>len(greet)

OUTPUT-
5

Congratulations! You successfully completed the tutorial on strings in Python.

In the upcoming tutorial, you will learn Python Lists.