📖 generic · 12th TN - English Medium · COMPUTER SCIENCE · Page 120definition

STRINGS AND STRING MANIPULATION · Part 3

Chapter 3: 4 · COMPUTER SCIENCE

recent call last): File "<pyshell# >", line , in <module> del str1[ ] TypeError: 'str' object doesn't support item deletion Example : Code lines to delete a particular character in a string: >>> str1="How about you" >>> print (str1) How about you >>> del str1 >>> print (str1) Traceback (most recent call last): File "<pyshell# >", line , in <module> print (str1) NameError: name 'str1' is not defined Example : Code lines to delete a string variable . 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 " 12th Computer Chapter - - Strings and String Manipulation >>> 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* ) 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 characters from a string, you have to specify it as to . Because, python consider only the end value as n- .

Example I : slice a single character from a string >>> str1="THIRUKKURAL" >>> print (str1[ ]) T

Related topics

Have a question about this topic?

Get an AI answer grounded in your actual textbook — with the exact page reference.

Ask AI about this topic →