Python Essentials Classroom Series – 05/Sept/2021

Strings continued

  • Get a character with []:

    • Python strings can be accessed by an offset Preview Preview Preview Preview
    • Since strings are immutable Preview
  • Getting a substring with a Slice:

    • We can extract substring (a part of string) by using a slice
    • slice will have the following [start:end:step] Preview
  • Getting length with len() Preview

  • Substitute by using replace() Preview

  • Strip with strip():

    • strip function get rid of whitespace character (‘ ‘, ‘\t’, ‘\n’) Preview
  • Search and Select:

    • Lets create a multi line string
    poem = """When you caught one to keep,
      we took it home and I asked you to teach me.
      You showed me how to spike the brain—
      I thanked the fish, looked away, pressed down.
      We bled it, shaved away the scales,
      severed meat from bone.
      I’m afraid of leaving my loved ones alone.
      Flying into an endless sunset the next day,
      a soft glow through the window,
      and every passenger is glazed
      a smooth bronze. Every other seat empty,
      each face masked, some with simple fabric, others
      medical-grade filtration set beneath serious eyes.

Preview Preview

  • Case: Preview

  • Formatting Strings:

    • This is also called as string interpolation
    name = "Ram"
    message = "Hello " + name +", How are you doing?"
    • But this approach is error prone, so in python we have f-strings
    name = "Ram"
    message = f"Hello {name}, How are you doing?"


Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About learningthoughtsadmin