The various string operations available in python are:

1.     Concat

     str1 = “ Hello “

     str2 = “ Hi “

     print ( str1 + str2 )

 

     Output:

     Hello Hi


2.     Repetition

     str1 = “ Hello “

     print ( str1 * 2 )


     Output:

     HelloHello


3.     Indexing

     str1 = “ HELLO “

 

     print ( str1 [ 1 ] )

     print ( str1 [ -4 ] )

 

     Output:

     E

     E

 

       0    1        3     4   

       H    E    L    L    O

      -5   -4    -3   -2   -1

 

4.     Slicing

 

     str1 = “ Hello “

     Statements:                                           Outputs:

     print ( str1 [ 0 : 4 ] )                              Hell       

     print ( str1 [ : ] )                                    Hello

     print ( str1 [ 4 : -1 ] )                            olleH

     print ( str1 [ 0 : 5 : 2 ] )                         Hlo

 

Methods in Strings:

1. len ()

    str1 = " Python "

    print ( len ( str1 ) )

    Output:

    6


2. replace ()

    str1 = " Python "

    print ( str1.replace ( "P" , "R" ) )

    Output:

    Rython


3. split ()

    str1 = " Python#Programming "

    print( str.split ( "#" ) )

    Output:

    [ " Python " , " Programming " ].


4. count ()

    str1 = " Rishi "

    print ( str1.count ( " i " ) )

    Output:

    2


5. upper () and lower ()

    str1 = " rishi " 

    str1 = " BHANSALI "

    print( str1.upper() )

    print( str2.lower() )

    Output:

    RISHI

    bhansali


6. strip ()

    str1 = " xzyz "

    print( str1.strip( 'z' ) )

    print( str1.rstrip ( 'z' ) )

    print( str1.lstrip( 'z' ) )

    Output:

    xy

    xzy

    xyz


7. find ()

    Finds the index of occurrence

    str1 = " Python "

    print ( str1.find( " t " ) )

    Output:

    2


8. isalnum()

    Returns true if all characters in the string are alphanumeric