Home | | Computer Science 12th Std | String Operators

Python - String Operators | 12th Computer Science : Chapter 8 : Core Python : Strings and String Manipulation

Chapter: 12th Computer Science : Chapter 8 : Core Python : Strings and String Manipulation

String Operators

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

String Operators

Python provides the following operators for string operations. These operators are useful to manipulate 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 I : slice a single character from a string

>>>str1="THIRUKKURAL"

>>>print (str1[0])

T

Example II : slice a substring from index 0 to 4

>>>print (str1[0:5])

THIRU

Example III : slice a substring using index 0 to 4 but without specifying the beginning index.

>>>print (str1[:5])

THIRU

Example IV : slice a substring using index 0 to 4 but without specifying the end index.

>>>print (str1[6:])

KURAL

Example V : Program to slice substrings using for loop

str1="COMPUTER"

index=0

for i in str1:

print (str1[:index+1])

index+=1

 

(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

Note: Remember that, python takes the last value as n-1

You can also use negative value as stride (third argument). If you specify a negative value, it prints in reverse order.

Example

>>>str1 = "Welcome to learn Python"

>>>print(str1[::-2])

nhy re teilW

 

Tags : Python , 12th Computer Science : Chapter 8 : Core Python : Strings and String Manipulation
Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
12th Computer Science : Chapter 8 : Core Python : Strings and String Manipulation : String Operators | Python


Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

Copyright © 2018-2024 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.