Python String with Example – Python Tutorial

3 weeks ago codedestine 0

In Python String are of Object type, this object contain sequence of letters in a specific order. For example, “Hello World” is a string as it contains sequence of letters h,e,l,o,w,r,d in a specific order like h letter is at first position and d letter is at last position.

In this tutorial, we will learn about the following:

  1. Creating Strings
  2. String Indexing
  3. String Properties
  4. String Slicing

Python String

Creating Strings

In Python string can be created by enclosing sequence of characters in single quotes or in the double quotes. For example:

# string enclosed in single quotes
 s = 'Hello World'

# string enclosed in double quotes
 s = "Hello World"

If you want to put single or double quotes within the string. You can do it, by enclosing string containing single quotes with double quotes and string containing double quotes with single quotes. For example:

# single quote inside double quotes
 s = "I'm using single quotes"

# double quote inside single quotes
 s = 'Using double quotes " " inside single quotes'

String Indexing

In python string are a sequence of letters in a specific order. We can use indexes to access individual parts of the sequence. In python, indexing starts from 0 and we use brackets [] after an object variable to access its index. For example:

# create one string and assign it to variable s s = 'Hello World' # accessing letter at index 0 print(s[0]) # accessing letter at index 1 print(s[1]) # accessing last letter of the sequence print(s[10]) # another way of accessing last letter of the sequence print(s[-1])

We can also use negative numbers for indexing. Indexing diagram for string ‘Hello World’ as follows: 

 H   e   l   l   o       W   o   r   l   d
 0   1   2   3   4   5   6   7   8   9   10
-11 -10 -9  -8  -7  -6  -5  -4  -3  -2  -1

String Properties

In python string are immutable, which means that once the string is created, all the elements ( letters ) with in a string can not be changed or deleted. For example:

# create one string and assign it to variable s s = 'Hello World' # change the first letter to 'y' s[0] = 'y' # click run button to see the result

String Slicing

In python string  can be sliced ( creating new sub string ) by using colon “:” operator inside brackets “[]“. 

        Syntax :  [ start index : end index : step ]
                      start index : slice of the given string will starts from this index, including letter at start index
                      end index : slice of the given string will end at this index, excluding letter at end index
                      step : it is a frequency of grabbing elements from the given string

For example :

# create one string and assign it to variable s s = 'Hello World' # create sub string 'llo World' print(s[2:]) # This prints a slice of the string, starting from index 2 and ending at last index # create sub string 'Hello' print(s[:5]) # This prints a slice of the string, starting from index 0 and ending at index 5 # create sub string 'llo Wo' print(s[2:8]) # This prints a slice of the string, starting from index 2 and ending at index 8 # create sub string 'loW' print(s[2:8:2]) # This prints a slice of the string, starting from index 2 and ending at index 8 and grab every alternate letter only # print complete string in reverse print(s[::-1])

References :-

  1. Strings Docs

That’s all for Python String with Example. If you liked it, please share your thoughts in comments section and share it with others too.

Next