STRING MANIPULATION

STRING MANIPULATION
Slide Note
Embed
Share

Strings in Python are sequences of characters enclosed in quotes. They are immutable, and an empty string has 0 characters. Learn about string indexes, traversing strings, string operators like concatenation and replication, and membership operators in Python.

  • Python
  • String Manipulation
  • Indexes
  • Operators
  • Membership

Uploaded on Mar 03, 2025 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. 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.

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

  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 The code will print: s-u-p-e-r-b- for i in name : print (i, - , end= ) -This loop will traverse through string name character by character.

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

  6. 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 Replication 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 Characters 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>)

More Related Content