Python Tutorial: Strings in Python

This is Python Tutorial: Strings in Python. In this Python Tutorial, you will learn the Strings in python language. At the end of this tutorial, you will have full knowledge of Strings.

Hello, & Welcome!

Part 3- Python Tutorial: Strings in Python

In this article of Python Tutorial, you will learn following-

  1. How to Represent Strings?
  2. Store String to Variable.
  3. String in Array Form.
  4. String Slicing.
  5. Most Used String Functions.

How to represent Strings?

String in Python is represented by single quotation marks (‘ ‘) or double quotation marks (” “).

For Example-

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

OUTPUT-
hello
hello

-> But there is one problem, suppose if you have to write- ‘He’s a good man’, this sentence gives a syntax error, because ‘He’ ends here.

->Therefore for apostrophe words use double quotation marks (” “). Let’s have a look-

print("He's is a good man")
OUTPUT-
He's is a good man
  • There is one method to use single quotation marks (‘ ‘) for apostrophe words, just put \ before apostrophe, like that- ‘he\’s a good man’.

->Let’s see in this example-

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

Store String to Variable-

We can store string to a variable in the same way we store numbers to a variable. Let’s have a look, how you can store string to a variable-

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

OUTPUT-
Hello

String in Array Form-

In Python, Strings are array of bytes, which represent Unicode characters. However, in Python, there is no Character Data Type, an individual character in python, is a string with a length of 1.

Like in an array, in strings, we use a square bracket [] to access elements.

-> If you want to get any specific element in a string, so you can it easily by just writing following line-

>>>greet[0]

OUTPUT-
'H'

->Here, we are taking greet=’Hello’.

->In this example, we are getting ‘H’, because we are giving index number 0.

Note- Index start from 0 to so on.

By Using Negative Indexing-

-> You can perform the same task from reverse order like ‘Hello’ <-, just using a negative index like -1.

Confused?… Let’s see in the example, to clear your confusion.

>>>greet[-1]

OUTPUT-
'O'                     

-> By passing -1 as an index, it is returning ‘o’, because, in negative indexing-

-1 represents ‘o’

-2 represents ‘l’

-3 represents ‘l’

-4 represents ‘e’

-5 represents ‘h’

NOTE- Backward indexing starts from -1 and forward indexing start from 0. For more clear understanding of indexing, Let’s have a look in 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 means, cut some parts from the whole piece. The same thing, we can do with string in python.

If we want to get only some parts from the word, for that purpose we do slicing.

So, are you ready to know how to perform slicing? Let’s get started.

  • Let’s consider the same example of greet=’Hello’, and if you want to cut this ‘Hello’ word, so you can do it by the following code-
>>> greet [0:3]

OUTPUT-
'Hel'

-> greet [0:3] means, it starts from index 0 to 2, it doesn’t take 3rd element. Let’s see in detail-

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

-> You can perform slicing with negative indexing, just have a look, how you can do it-

>>>greet [2:-1]

OUTPUT-
'll'

-> You are wondering, how it return, ‘ll’ as a result. Don’t worry, I will explain to you, how, ‘ll’ come in output.

-5  -4  -3  -2  -1    
H    e   l   l   o
0    1   2   3   4
        -------          
  • greet[2 : 1], starts from Index 2, that is ‘l’ to Index before -1, that is Index -2, and at Index -2, the word is ‘l’, that’s why it is returning as, ‘ll’.

-> By doing all these operations, the value of greet is still ‘Hello’.

-> If you want to permanently change the value of greet variable, then you need to reassign it. Let’s have a look at how you can reassign.

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

OUTPUT-
'Hel'

Most Used String Functions

In String, you can perform various operations, here we discuss some most used and important functions. Let’s have a look at these functions.

String Concatenation-

->If you want to concatenate or add two strings, then you can do it simply by using ‘+’ sign. Let’s see in this example-

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

OUTPUT-
'HelloJohn'
  • There is no space between Hello and John, so if you want to give space, just give ‘ ‘. See in the example below-
>>>greet+' '+str

OUTPUT-
'Hello John'

Repetition of Strings-

If you want that one word repeats multiple times, so use * sign with the number you want to repeat that word. Let’s see in the example below-

>>>greet*3

OUTPUT-
'HelloHelloHello'

Note- To use any string method, just use the ( . ) operator.

String UPPER CASE-

->If you want to convert a string from lower case to upper, use the upper() method. Let’s see in the example.

>>>greet.upper()

OUTPUT-
'HELLO'
  • For converting the upper case to lower case, just use a lower() method.

String Splitting-

->If you want to split a different string, you can do it by writing a split(‘,’) method. Let’s see in the example below-

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

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

String Length-

-> To find the length of string, write len(string) function. For example-

>>>len(greet)

OUTPUT-
5

And here we go, congratulations! You successfully learned strings in python.

Python Tutorial: Strings in Python

In the next tutorial, we will start learning Python Lists.

Are you looking for Best Books on Python for Beginners and for Intermediate/Experts?

Check these Books, These Books are the best selling and covered everything in detail.

I hope you will find these Books helpful-

Best Selling Python Books for Beginners

Best Selling Python Books for Intermediates/Experts

Enjoy Learning Python!

All the Best!

Back to Python Tutorial

Thank YOU!

Though of the Day…

I The more I read, the more I acquire, the more certain I am that I know nothing.’ ”

– Voltaire

8 thoughts on “Python Tutorial: Strings in Python”

  1. Pingback: Python Tutorial: Numbers in Python- Beginners Guide(2020)

  2. Pingback: Python Tutorial: List in Python- Beginners Guide(2020)

  3. Fantaѕtic pօst һlwеver , I was wondering if youu could write a litte more on thiѕ topic?

    I’d be very thankful if you could elaborate a ⅼіttle bit more.
    Thank you!

  4. Thank you so much dear. I was really looking for such kinda content from a long time and today i found it. Great work. Carry on.

Leave a Comment

Your email address will not be published. Required fields are marked *