Presentation is loading. Please wait.

Presentation is loading. Please wait.

By Himanshi dixit 11th ’B’

Similar presentations


Presentation on theme: "By Himanshi dixit 11th ’B’"— Presentation transcript:

1 By Himanshi dixit 11th ’B’
STRING MANIPULATION By Himanshi dixit 11th ’B’

2 INTRODUCTION Strings are sequence of characters.
Strings are enclosed in quotes of any type. Ex:- ‘string’, “string” and ‘‘‘string’’’ Strings are immutable. An empty string is a string that has 0 characters. Ex:- “” Each character of string has a unique position-id /index. P T H O

3 INDEX The indexes of a string begin from 0 to (length-1) in forward direction and -length in backward direction P Y T H O N

4 TRAVERSING A STRING Traversing refers to iterating through the elements of a string one character at a time. Individual characters of a string are accessible through the unique index of each character. Example:- name=‘superb’ for i in name : print (i, ‘-’, end=“ “) -This loop will traverse through string name character by character. The code will print: s-u-p-e-r-b-

5 String operators Basic operators
Examples Basic operators The two basic operators of strings are : + and *. String concatenation operator + The + operator creates a new string by joining the two operand strings, e.g., “tea”+ “pot “ Will result into- “teapot” Expressions Will result into ‘1’+’1’ ‘11’ “a”+”0” “a0” ‘123’+’abc’ ‘123abc’ NOTE : The + oprator has to have both operands of the same type either of number types or of string types.

6 * Operator performs replication rather than multiplication in strings.
STRING REPLICATION OPERATOR * * Operator performs replication rather than multiplication in strings. For replication operator ,python creates a new string that is a number of repetitions of the input string. Eg :- 3* “go! ” will return “go!go!go! ” The * operator has to either have both operands of the number types (for multiplication)or one string type and one number type (for replication). It cannot work with both operands of string types.

7 Working of python * operator
Operands data type Operation performed by * Example Numbers Multiplication 9 * 9=18 String , Number Replication ‘#’ * 3= ‘###’ Number , String 3 * ’#’=‘###’

8 MEMBERSHIP OPERATORS There are two membership operators for strings i.e. in and not in. Membership operators (when used with strings) , require both operands used with them are of string type, i.e. <string> in <string> <string> not in <string> Eg: “12” in “xyz” “12” not in “xyz”

9 COMPARISON OPERATORS Python standard comparison operators i.e., all relational operators (<,<=,>,>=,==,!=) apply to strings also. Eg: “a” == “a” will give true “A” != “a” will give true “ABC” == “abc” will give false For comparisons like less than (<) or greater than (>) , common characters and their ordinal values should be known.

10 Common characters and their ordinal values
‘0’ to ‘9’ 48 to 57 ‘A’ to ‘Z’ 65 to 90 ‘a’ to ‘z’ 97 to 122 ‘a’ < ‘A’ will give false ‘abc’ <= ‘ABCD’ will give false

11 Determining ordinal value of single character
Built -in function ord() that takes a single character and returns the corresponding ordinal Unicode value. ord ( <single -character>) Eg: ord ( “A”) 65 The opposite of ord() function is chr() The chr() takes the ordinal value in integer form and returns the character corresponding to that ordinal value. chr(<int>)

12 Thank You


Download ppt "By Himanshi dixit 11th ’B’"

Similar presentations


Ads by Google