Ad Code

Responsive Advertisement

Strings in Python Class 11 | String Manipulation Notes

  

What is Strings?

  • String is basically a sequence which is made up of one or more UNICODE characters.
  • Character in string can be any letter, digit, whitespace or any other symbol.
  • String can be created by enclosing one or more characters in single, double or triple quotes.


INDEXING – Accessing characters in a string

  • Individual character in a string can be accessed using indexes.
  • Indexes are unique numbers assigned to each character in a string to identify them
  • Index always begins from 0 and written in square brackets “[]”.
  • Index must be an zero, positive or negative integer.
  • We get IndexError when we give index value out of the range.


Negative Indexing
  • Python allows negative indexing also.
  • Negative indices are used when you want to access string in reverse order.
  • Starting from the right side, the first character has the index as -1 and the last character (leftmost) has the index –n where n is length of string.
Is string immutable?
  • Yes, string is immutable data type. The content of string once assigned cannot be altered than.
  • Trying to alter string content may lead an error.

String operations

String supports following operations:

  • Concatenation
  • Repetition
  • Membership
  • Slicing
Concatenation
  • Concatenation refers to joining two strings.
  • Plus (‘+’) is used as concatenation operator.

Repetition
  • Repetition as it name implies repeat the given string.
  • Asterisk (‘*’) is used as repetition operator.

Membership
  • Membership operation refers to checking a string or character is part or subpart of an existing string or not.
  • Python uses ‘in’ and ‘not in’ as membership operator.
  • ‘in’ returns true if the first string or character appears as substring in the second string.
  • ‘not in’ returns true if the first string or character does not appears as substring in the second string.

Slicing
  • Extracting a specific part of string or substring is called slicing
  • Subset occurred after slicing contains contiguous elements
  • Slicing is done using index range like string[start_index : end_index : step_value]
  • End index is always excluded in resultant substring.
  • Negative index can also be used for slicing.

Traversing a String

  • Traversing a string refers to accessing each character of a given string sequentially.
  • For or while loop is used for traversing a string


String Functions


Post a Comment

0 Comments