String Operators  in Python

String Operators in Python

Author:
Price:

Read more

 String Operators in Python


Programming in Python

                                           In Python, programs can be written in two ways namely Interactive mode and Script mode. The Interactive mode allows us to write codes in Python command prompt (>>>) whereas in script mode programs can be written and stored as separate file with the extension .py and executed. Script mode is used to create and edit python source file.

 String Operators

 Python provides the following operators for string operations. These operators are useful to manipulate string 

 i. Concatenation (+)

ii.  Append (+ =)

iii. Repeating (*)

iv. String slicing

v.  Stride when slicing string


i. Concatenation (+)

                 Joining of two or more strings is called as Concatenation. The plus (+) operator is used to concatenate strings in python.

Example:

                    >>> "welcome" + "Python"

                                        'welcomePython'

ii.  Append (+ =)

                       Adding more strings at the end of an existing string is known as append. The operator += is used to append a new string with an existing string.

Example:

                >>> str1="Welcome to "
                >>> str1+="Learn Python" 
                >>> print (str1) 

                              Welcome to Learn Python

 

iii. Repeating (*)

                                            The multiplication operator (*) is used to display a string in multiple number of times.

Example:

                >>> str1="Welcome " 
                >>> print (str1*4) 

                                      Welcome Welcome Welcome Welcome

iv. String slicing

                      Slice is a substring of a main string. A substring can be taken from the original string by using [ ] operator and index or subscript values. Thus, [ ] is also known as slicing operator. Using slice operator, you have to slice one or more substrings from a main string. 
              General format of slice operation: 

                             str[start:end] 

Where start is the beginning index and end is the last index value of a character in the string. Python takes the end value less than one from the actual index specified. For example, if you want to slice first 4 characters from a string, you have to specify it as 0 to 5. Because, python consider only the end value as n-1.

Example:1

             >>> str1="THIRUKKURAL" 
             >>> print (str1[0])

                                        T

Example:2

              >>> str1="THIRUKKURAL"
              >>> print (str1[0:5])

v.  Stride when slicing string

                          When the slicing operation, you can specify a third argument as the stride, which refers to the number of characters to move forward after the first character is retrieved from the string. The default value of stride is 1.

Example:

              >>> str1 = "Welcome to learn Python" 
              >>> print (str1[10:16])

                                       learn

              >>> print (str1[10:16:4])

                                           r 

              >>> print (str1[10:16:2])

                                           er 

              >>> print (str1[::3]) 

                                 Wceoenyo

0 Reviews