Python Strings

Create, index and slice strings.

✍️ 3 exercises + code challenge in this lesson

Strings are written in single or double quotes. Triple quotes create multi-line strings.

  • Strings are sequences: s[0] is the first character, s[-1] the last.
  • Slicing: s[2:5] gets characters 2 to 4; s[:3], s[3:], s[::-1] (reversed).
  • len(s) returns the length.
  • in checks for a substring: "py" in "python".
  • Strings are immutable: you cannot change a character in place.

Key Takeaways: Python Strings

Mastering Python Strings helps you write clean and reliable Python code. Follow standard conventions, test edge cases, and keep logic simple. Try running the code example below and complete the exercises to build confidence.

Python Strings Code Example

Python
s = "CodesInTech"
print(s[0], s[-1])
print(s[0:5])
print(s[5:])
print(s[::-1])
print(len(s))
print("Tech" in s)

for ch in "abc":
    print(ch)
Free sign-in required · Google login available

Python Strings Practice Exercises

Check what you learned in “Python Strings”.

/ 3 solved
Fill in the blank

Get the last character of the string.

last = word[  ]
✓ Correct! Not quite. Try again.
Multiple choice

What does "Python"[0:2] return?

✓ Correct! Not quite. Try again.
Code challenge

Print the word reversed using slicing.

Write the code in the live editor. Your answer is checked automatically.

Free sign-in required · Google login available

to save your exercise progress.
Finished this lesson?
Mark it complete to track your progress.

Stuck on something?

Ask the community in the CodesInTech forum.

Need a developer?

Learning on your own is great — and when you need professional help, our team builds custom software, websites and mobile apps for clients worldwide.

Explore Our Services