String Operations

369 Tesla - Kalviyogi Nagarajan

What Are Strings?

Strings in Python are like containers for text! They're sequences of characters enclosed in quotes. Think of them as words or sentences that your computer can understand and work with.

In everyday life, strings are like the text messages you send to friends, the names of your favorite games, or the words in your favorite book!

Key Features of Strings

  • 1

    Immutable:

    Strings cannot be changed after creation. Any change makes a brand new string!

  • 2

    Indexed:

    Each character has a unique position (index), starting at 0.

  • 3

    Slicing:

    You can extract pieces of strings using slice notation.

Try It Yourself!

My_Str = "Tesla"print(My_Str[0]) # Output: Tprint(My_Str[-1]) # Output: a

What's happening here?

In this example, we've created a string "Tesla" and accessed specific characters. The index [0] gives us the first character, and [-1] gives us the last character!

Visualizing Strings

T
e
s
l
a
0
1
2
3
4
-5
-4
-3
-2
-1

Remember: In Python, counting starts at 0, not 1! So the first character is at position 0, the second at position 1, and so on.