Session – 9(Strings 1-2)

  • Strings
  • Escape Characters
  • String Operators
  • String Formatting Operator

1.Strings

A string is a sequence of characters.

  • A type in python
  • ‘ ‘ or ” “
  • Single quotes
  • Double quotes
  • s ← ‘ ‘ or ” “
  • s[x] – xth Chr
  • s[x:n] – xth → n — Chr’s

2.Escape characters

To insert characters that are illegal in a string, use an escape character .An escape character is a backslash \ followed by the character you want to insert.

  • Non-printable characters
  • Backslash notation “\”
  • Single quoted strings → ‘ ……………….………..’
  • Double quoted strings → “ ……………….………..”
  • \n
  • \t
  • r’..’

Other escape characters used in Python:

CodeResult
\’ Single Quote
\\ Backslash
\n New Line
\r Carriage Return
\t Tab
\b Backspace
\f Form Feed
\ooo Octal value
\xhh Hex value

3.String Formatting Operator

One of Python’s coolest features is the string format operator %. This operator is unique to strings and makes up for the pack of having functions from C’s printf() family.

  • %c : Chr
  • %s : Str
  • %i : Int
  • %d : Int
  • %u : Unsigned int
  • %o : Octal int
  • %x : Hexa (x or X)
  • %e : Exp (e or E)
  • %f : Float pt num
  • %g : Shorter

Leave a comment