Python string Datatype

String Datatype : A sequence of character is called a string, which may contain literal constants or some kind of variables. The String is always represented in double-quotes.

Example: "Chercher. tech", "Banana", "Apple","One", etc..

Importance of string Datatype

  • In python, the most commonly used data type is string only.
  • The default return type of input function is always a string type only.
  • By default, the command line arguments are a string only.
  • The single character within a single quote or double quote is also considered as string datatype only.
    ex: ch='A', ch="B",etc..
  • We can define the multi-line literals by using either triple single quotes or triple double-quotes.
    Example: """Elepbhant                       '''Parrot
                      Tiger                                  Crow     
                            Lion"""                               Roaster'''

Ways to Access the String Characters

We can access the String by using the following two ways

Index:

The index is used to access the character of the string, the value of the first character of the index is always zero (0), let us consider an example as following

s="dhanvik"
print(s[0])=?
print(s[1])=?
print(s[4])=?

In the above example the value of d=0,h=1,a=2,n=3,v=4,i=5,k=6 and if you print the value of above character

index-values

In the above example, the index value is up to 6 and if I am going to print as below then you will get an index error

print(s[100])

index-error

One more very important thing is that python will provide support for both positive indexing and negative indexing, that means from backward direction index value will start from -1 so the value of k=-1,i=-2,v=-3,n=-4,a=-5,h=-6,d=-7

s="dhanvik"
print(s[-1])=?​
print(s[-5])=?

negative-indexing

Slice operator :

In string we have an operator is called Slice, which is nothing but a part of a string, in some cases, if we want to print a part of that string or a slice of that string then we will use this operator.

The syntax of the slice operator is

s[begin:end:step]

Begin : The integer where the beginning of a slicing object will start.

End : The integer until which the slicing will take place or slicing will stop.

Step : Integer which determines the increment between each index for slicing.

  • The step value can be either +ve or -ve
  • If the step value is +ve then, we have to consider from the begin to end-1 in Forward direction(From left to right)
  • If the step value is -ve then, we have to consider from the begin to end+1 in Backward direction(From right to left)
  • In the backward direction if the step value -1 then the result is always empty.
  • In the forward direction if the step value 0 then the result is always empty.
  • In Forward Direction:
    The default value for begin is 0
    The default value for the end is the length of the string
    The default value for step is +1
  • In Backward Direction:
    The default value for begin is -1
    The default value for the end is -(length of the string+1)
  • We can consider both +ve and -ve values for beginning and end index in case of forwarding and backward directions.

The following example to demonstrate slice operator.

S ='abcdefghij'
Below are the inputs and outputs
s[1:6:2] => 'bdf'
s[::1] =>'abcdefghij'
s[::-1] =>'jihgfedcba'
s[3:7:-1] => ''
s[7:4:-1] => 'hgf'
s[0:10000:1] =>'abcdefghij'
s[-4:1:-1]=> 'gfedc'
s[-4:1:-2] =>'gec'
s[5:0:1] =>''
s[9:0:0] =>ValueError: slice step cannot be zero
s[0:-10:-1]=> ''
s[0:-11:-1] =>'a'
s[0:0:1] =>''
s[0:-9:-2] => ''
s[-5:-9:-2] =>'fd'
s[10:-1:-1] =>''
18)s[10000:2:-1] => 'jihgfed'

slice-operator-one

slice-operator-two

Example: Here s="abcdefghiklmnopqrstuvwxyz"
And the index value will start from zero and hence a=0, b=1, and c=3, etc... z=25.

s="abcdefghiklmnopqrstuvwxyz"
print(s[3:9])

Then the above example will return from the third index to 9-1, that means up to 8 index

slice-indexing-one

example:2

s="abcdefghiklmnopqrstuvwxyz"
print(s[ :9])

In the above example am going to mention only the end argument and begin argument is empty, so in this case, it will going to consider by default from the beginning itself

Concatenate and Repeat Operators for String

We can use+operator for concatenation and * operator for repeating strings.

Concatenation:

Concatenation is nothing but adding or joining two strings, To perform concatenation, both the arguments must be of string type. We are going to use Plus(+) symbol as concatenation operator.

Example:

s="dhan"+"vik"
print(s)
s="dhan"+"10"
print(s)
s="dhan"+10
Print(s)

concatenation-operation

Example:

print("chercher"+"tech")

string-mathematical-operator

Repetition Operator:

Repetition: Some times we want to write the same text multiple times, instead of writing it manually we can use the repetition operator to repeat the same text as many times as you want to print.

To perform the repetition, one argument should be a string and another must be of int datatype, Here we are using the Multiplication operator or a star operator to repeat the text.

Example:

s="cherchertech."*3
print(s)

repetition-operator-four

Example:

print("Apple"*3)

string-mathematical-operator-one

How to Concatenate String and Other Datatypes

We have several ways to concatenate different data types

1.By converting the non-string to the string data type

Example:

int=56
print("Health is wealth"+str(int))

concatenating-non-string-one

2. By using the interpolation, we will represent interpolation by using % symbol

answer = 42
print('The cost of six banana is %d' % (answer))

interpolation-operator-one

3.Using the format()method

answer = 42
print('The cost of six banana is {}'.format(answer))


uding-format-method-one

Substring in Python

Substring: A part/sequence of String is called as a substring.

example: 
main string: "wonder is wonderful"
substring1: "wonder"
substring2: :won"
substring3: "is"
substring4: "wonderful"
substring5: "ful"

Python supports string to check whether the given character or a string is a member of any string or not by using the in and not operators.

Example:1

s="Strong"
print("S" in s)

membership-operator-one

Example:2

s="Trust"
print("o" not in s)

membership-operators-three

The following example to demonstrate membership operator

s = input("Enter main string:")
subs = input("Enter sub string:")
if subs in s:
    print(subs,"is found in main string")
else:
    print(subs,"is not found in main string")

If the main string is chercher.tech technologies

membership-operators-four

If the substring is not present in the main string, then the output is

membership-operators-five

Comparison of String

We can compare the strings using the comparison/relational operators.

  • Equal (==)
  • Not equal (!=)
  • Greater than (>)
  • Less than (<)
  • Greater than or equal to (>=)
  • Less than or equal to (<=)
Example: 2!=4, 8==2*4, 6>4,etc..

The following example is to compare the two strings, python compares the two strings by comparing each character based on their Unicode values.

s1=input("Enter first string:")
s2=input("Enter Second string:")
if s1==s2:
    print("Both strings are equal")
elif s1<s2:
    print("First String is less than Second String")
else:
    print("First String is greater than Second String")

By comparing the moon and sun the python will return as Moon<Sun because of the Unicode value of S is greater than the Unicode value of M, we can get the Unicode of each character by using print(ord("M")) and print(ord("S"))

comparison-operator-two

And hence the output of the comparison program is as below

comparison-operator-one

If we compare India and India, then the output is

comparison-operator-three

String Methods in Python

Python has some built-in methods which we can use on strings to perform some operations.

Remove spaces in String

Python provides us with some inbuilt functions to remove the spaces from a string

  • rstrip(): To remove spaces at the right-hand side
  • lstrip() : To remove spaces at the left-hand side
  • strip() : To remove spaces both sides

Example without Removing spaces from the string

city=input("Enter your city Name:")
if city=='Hyderabad':
    print("Hello Hyderbadi..Adab")
elif city=='Chennai':
    print("Hello Madrasi...Vanakkam")
elif city=="Bangalore":
    print("Hello Kannadiga...Shubhodaya")
else:
    print("your entered city is invalid")

If I enter the city name as Bangalore without spaces, then the output is

string-methods-one

And I enter the city name beginning with space as Hyderabad then the output is

string-methods-two

Python will not remove any spaces before or after the given string, by using a method strip() we can remove the spaces.

We are assigning stripped value to a variable called scity as scity=city.strip()

city=input("Enter your city Name:")
scity=city.strip()
if scity=='Hyderabad':
    print("Hello Hyderbadi..Adab")
elif scity=='Chennai':
    print("Hello Madrasi...Vanakkam")
elif scity=="Bangalore":
    print("Hello Kannadiga...Shubhodaya")
else:
    print("your entered city is invalid")

If I enter the city name with space in the beginning and as well as spaces at the end, then the output will be.

string-methods-three

To Find the Index of given Substring

Python also provides some in-built methods to find the index of the given substring,

There are four methods we have to find the substring

find()= Used to find the substring in forward direction.

rfind()=Used to find the substring in forword direction.

index= Used to find the substring in a backward direction.

rindex()=Used to find the substring in a backward direction

Example:

s="ABCD"
print(s.find("B"))

If B is present in the given string then, python's find() method will return its index value as 1. The find method is always going to search from right to left.

find-method-one

If the given substring is not present in the string then find() method will return -1.

find-method-two

And, if we pass the rfind() method to find the substring B from the given string s="ABCDBA", The rfind() method will always search from left to right so it will return index value as 4.

s= "ABCDBA"
print(s.find("B"))

find-method-three

In the case of rfind() method also if the given substring is not present in the string, then the python will return index value as -1 only.

find-method-four

If the main string s="ABCDEFGH" and we have to find the substring in the range between D to G then we can use the find() method with index range as print(s.find(substring,begin,end))

s="ABCDEFGH"
print(s.find("B",3,6))

find-method-five

The Given substring "B" is not present in the given index range and hence python will return -1.

If the print(s.find("F",3,6)) then the output is

find-method-six

In the same way, we can access the substring by using the index() method also

s="IJKLMNOPQRST"
print(s.index("O"))

find-method-seven

If the substring is not present in the given string the index() is going to throw a value error

find-method-eight

Traceback (most recent call last):
  File "C:UsersAnup kumareclipse-workspaceFirstProjectPythonFirstPythonPackageFirstPythonModule.py", line 2, in <module>
    print(s.index("A"))
ValueError: substring not found

Similarly, if we use rindex() method to find the substring R in the given string, python will search from left to right and returns as 8.

s="PQRSTUVWRX"
print(s.rfind("R"))

find-method-nine

The following example demonstrates the index() method

mail=input("enter your mail Id:")
try:
    i=mail.index("@")
    print("Mail id contains @ symbol, which is mandatory")
except ValueError:
    print("Mail id does not contain @ symbol, which is mandatory")

The entered mail id does not contain @ symbol then, the output is

find-method-ten

The entered mail id contain @ symbol then, the output is

find-method-eleven

Counting the substring in the given string

Counting: It is the process of determining the number of elements of a finite set of objects.

Python provides an in-built function count(), which returns the number of occurrences of a substring in the given string.

The syntax for the count() method is

string.count(substring)

The following example demonstrates the count() method

s="ABBABBAABBABA"
print(s.count("B")

The count() returns the total number of B in the given string.

count-method-one

To count the BB in the given string count-method-two

If the given substring is not present in the string then, python will return 0.

count-method-three

To count the substring in the given index print(s.count("B",4,100)), then the output is

count-method-four

To find the index of all the occurrence of the given substring

s="ABCABCABCA"
subs=input("Enter substring to search:")
i=s.find(subs)
if i==-1:
    print("specified substring not found")
while i!= -1:
        print("{} present at index:{}".format(subs,i))
        i= s.find(subs,i+len(subs),len(s))

The output is

count-method-fiver

We can make substring to enter dynamically as

s="ABCABCABCA"
subs=input("Enter substring to search:")
i=s.find(subs)
if i==-1:
    print("specified substring not found")
while i!= -1:
        print("{} present at index:{}".format(subs,i))
        i=s.find(subs,i+len(subs),len(s))

The output is

count-method-six

If the entered search string is BCA then, the output is

count-method-seven

If you want to know the number occurrences by using s.count(subs)

s="ABCABCABCA"
subs=input("Enter substring to search:")
i=s.find(subs)
if i==-1:
    print("specified substring not found")
while i!= -1:
        print("{} present at index:{}".format(subs,i))
        i= s.find(subs,i+len(subs),len(s))
print("The number of occurrences:",s.count(subs))

Then the out is

count-method-eight

The following example to replace the string with another string by using the replace() method

The syntax for replace() method is string.replace(oldstring, newstring)

s="ABABABA"
s1=s.replace("A", "B")
print(s1)

In the above example, A is replaced with B and the output is:

count-method-nine

By using the replace() method we can also remove the spaces between the string as follow

s="cher cher tech"
s1=s.replace(" ","")
print(s1)

The replace () method will replace the " " with ""

count-method-ten

We can also count the number of spaces between the string by using the count() method

s="cher cher tech"
s1=s.replace(" ","")
print("The Number of spaces :",s.count(" "))

The output is

count-method-eleven

We can also use another one method to find the number of spaces between the strings instead of count() method

s="cher cher tech"
s1=s.replace(" ","")
#print("The Number of spaces :",s.count(" "))
print("The Number of spaces:" , len(s)-len(s1))

count-method-twelve

The replace() is case sensitive

s="Learning Python is Very Difficult"
s1=s.replace('Difficult", "Easy")
print(s1)

The output is

count-method-thirteen

If we enter the difficult instead of Difficult then the replace() method won't replace and returns as it is.

count-method-fourteen

All the string objects in Python are immutable, but we are replacing the string by using the replace() method by creating a new object.


The proof is :

count-method-fifteen

If we print the address of sand s1

s="ABABAABA"
print("Before Replacement address of s:", id(s))
s1=s.replace("A","B")
print("After Replacement address of s:", id(s1))

count-method-sixteen

Splitting and Joining of given String

Splitting: The action of dividing into parts is called splitting.

Example: The Wooden floor had cracked and split in the heat. 

Joining: Connecting the two things together is called as joining.

Example: The two pieces of cloth are joined with velcro

Splitting of Strings : Python provides an in-built split() method which splits the strings into a List.

The syntax is

s.split()

The return type of split() method is always List.

The following example demonstrates the use of a split() method

s="Cher Cher Tech"
l=s.split()
print(l)

split-method-one

The Default separator for the split() method is white space, if we want to split the given string according to any other separator then we have to pass the separators in arguments.

d="05-04-2019"
l=d.split("-")
for x in l:
    print(x)

split-method-two

Even we can decide that the given string should be divided into a particular number of elements by setting the parameter. If we set the parameter to 1 then python will return the list with two elements.

txt = "apple#banana#cherry#orange"
x=s.split("#",1)
print(x)

split-method-three

Joining of String: The join() is a string method which returns the string when the elements of the string are joined with string separator.

The syntax for the join() method is

s = seperator.join(iterable)

The following example demonstrates the join() method

mytuple=("cher", "cher", "tech")
x="#".join(mytuple)
print(x)

joint-method-one

Another example demonstrates the join() method

l=['04', '05', '2019']
s='-'.join(l)
print(s)

joint-method-two

Changing the Case of characters of the String

Case sensitive: The differentiation between the entered characters whether they are in uppercase or Lowercase.

If your programming language is case sensitive, which expects you to enter an uppercase letter won't respond if you enter lowercase letters.

  • Uppercase letters: ABCDEFGH
  • Lowercase Letters: abcdefgh

Python provides five types of methods to change the case of the string

upper(): to Convert all the characters to uppercase

lower() : to convert all the characters to lowercase

swapcase(): Convert all the lowercase characters to uppercase and convert all the uppercase characters to lowercase.

title(): To convert all character to title case, means the first character in every word should be upper case and all remaining characters should be in lower case.

capitalize(): Only first character will be converted to upper case and all remaining characters can be converted to lower case.

The following example demonstrates changing the case of characters of the string

s = 'learning Python is very Easy'
print(s.upper()) 
print(s.lower()) 
print(s.swapcase())  
print(s.title())
print(s.capitalize())

The output is

changing-case-of-charectaers-of-string

A program to check whether the given two strings are equal or not by ignoring case

s1=input("Enter First string:")
s2=input("Enter Second String:")
if s1==s2:
    print("Both strings are equal:")
else:
    print("Both the strings are not equal")

If we compare Review and review then, python returns strings are not equal because these two strings are end-user given strings, if both the strings are same but the case is different.

changing-case-of-charectaers-of-string-one

To overcome this problem we have to convert the end user given strings into either uppercase or lower case and then compare the two strings as shown below

s1=input("Enter First string:")
s2=input("Enter Second String:")
if s1.lower()==s2.lower():
    print("Both strings are equal")
else:
    print("Both the strings are not equal")

The output is

changing-case-of-charectaers-of-string-two

And, if we enter two different strings then, the output is

changing-case-of-charectaers-of-string-three

We can shorten the above program code by converting the string to either lower or uppercase at the beginning itself.

s1=input("Enter First string:").lower()
s2=input("Enter Second String:").lower()
if s1==s2:
    print("Both strings are equal")
else:
    print("Both the strings are not equal")

The output is

changing-case-of-charectaers-of-string-four

The program is to check whether the provided username and password are valid or not(username should not be case sensitive but the password should be the case sensitive.

username=input("Enter user Name:")
pwd=input("Enter Password:")
if username.lower()=="dhanvik" and pwd=="arrow":
    print("Valid User")
else:
    print("Invalid User")  

The username is irrespective of case sensitive and password is the case sensitive

case-sensitive-one

case-sensitive-two

If we enter the password as Arrow, then python will immediately throw an error case-sensitive-three

The program to convert the first and last character as upper case and all remaining characters should be the lower case of the given string

s=input("Enter any string:")
output=s[0].upper()+s[1:len(s)-1].lower()+s[-1].upper()
print(output)

The output is

case-sensitive-four

Checking starting and end part of the string

There are two methods to check whether the given string starts with the substring

  • s.startswith(substring): Returns true if the string starts with provided substring.
  • s.endswith(substring): Returns true if the string ends with provided substring.

The following example to

s='Learning python is very easy'
print(s.startswith('Learning'))
print(s.endswith('easy'))

The output is:

checking-starting-and-end-part-of-the-string

Checking the different types of characters present in the string

Python provides seven types of Methods to check types of characters present in the string

isalnum(): Returns True if all characters are alphanumeric( a to z, A to Z, 0 to9 )

isalpha(): Returns True if all characters are only alphabet symbols(a to z, A to Z)

isdigit(): Returns True if all characters are digits only( 0 to 9)

islower(): Returns True if all characters are lower case alphabet symbols

isupper() : Returns True if all characters are upper case alphabet symbols

istitle() : Returns True if the string is in title case

isspace() : Returns True if the string contains only spaces

The following example demonstrates the use of all the above-mentioned Methods

print('Sun786'.isalnum())=>True
 print('east786'.isalpha()) =>False 
 print('west'.isalpha())=>True 
print('bengal'.isdigit()) => False 
 print('786786'.isdigit()) =>True
 print('abc'.islower()) =>True 
print('Abc'.islower())=> False 
 print('abc123'.islower()) =>True 
print('ABC'.isupper()) => True 
 print('Learning python is Easy'.istitle())=> False 
 print('Learning Python Is Easy'.istitle()) => True 
 print('    '.isspace()) => True 
  • In the above program, the first line contains both alphabets and numbers and hence python return True
  • Next line should contain only alphabets, but here both alphabets and numbers are present and hence python returns False.
  • The west contains only alphabets and hence python returns True.
  • The bengal contain only alphabets but not numbers, python returns False.
  • isdigit() method checks for only digits, and hence returns True.
  • islower() method checks for lowercase alphabets and hence python returns True.
  • isupper() method checks for upper case alphabets and returns True.
  • istitle() method checks for title case if the first character of every word in the string is in uppercase and then python returns True or else returns false.

The output is

ctcps-one

The following program to check the type of character entered from the keyboard

s=input('Enter any character:')
if s.isalnum():
    print('It is alphanumeric character')
    if s.isalpha():
        print('It is alphabet character')
        if s.islower():
            print('It is lowercase alphabet character')
        else:
            print('It is uppercase alphabet character')
    else:
        print('It is a digit')
elif s.isspace():
                print('It is space character')
else:
                print('It is non space special character')

If the input is A, then the output will be

ctcps-two

And, if the input is 6, then the output is

ctcps-three

IF the input is a lowercase letter, then the output is

ctcps-four

The String Programs

1. Write a program to reverse the content of a given string

string="reverse"
output=string[::-1]
print(output)

The output is

program-one

And if you want to enter any string dynamically

string=input("Enter any string:")
output=string[::-1]
print("Reversed string:", output)

The output is:

program-one-one

2. write a program to reverse the content of the given string by using a reversed() function

string="gold"
r=reversed(string)
output=''.join(r)
print(output)

program-two

3. Program to reverse the content of the given string by using the while loop

string=input("Enter the string to reverse:")
output=''
i=len(string)-1
while i>=0:
    output=output+string[i]
    i=i-1
print(output)

program-three

4. Program to reverse the order of the words present in the given string

string=input("enter some string")
l==s.split()
l1=l[;;-1]
output=' '.join(l1)
print(output)

123455432

Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions