9. Introduction to Strings

Quiz: 0/5

We’re going to learn about strings in Python. Strings are one of the most basic and essential data types in programming, especially when working with text. By the end of this, you’ll understand what strings are, how to create and use them, and avoid common mistakes.

What are strings?

A string is simply a sequence of characters enclosed in quotes. Think of it as a piece of text that Python recognizes and works with.

Examples:

  • "Hello, world!" (a string in double quotes)
  • 'Python is fun!' (a string in single quotes)
  • """This is a longer string.""" (a string in triple quotes)

Anything inside these quotes is treated as a string in Python.

How to create strings

There are three ways to define a string:

  1. Single quotes: 'This is a string'
  2. Double quotes: "This is also a string"
  3. Triple quotes: '''This is a string too''' or """Another example"""

Triple quotes are especially useful for strings that span multiple lines.

Playing with strings

Joining strings (concatenation):

You can combine strings using the + operator.

greeting = "Hello, " + "world!" print(greeting) # Output: Hello, world!

Repeating strings:

You can repeat strings using the * operator.

laugh = "ha" * 3 print(laugh) # Output: hahaha 

Accessing characters in Strings

Strings are like a row of letters. You can pick a specific letter (or character) by its position (index).

word = "Python" print(word[0]) # Output: P (the first letter) print(word[3]) # Output: h (the fourth letter) 

Indexes start at 0 in Python:

  • word[0] is the first character.
  • word[1] is the second, and so on.

Slicing strings:

You can get a part of a string using slicing.

 word = "Python" print(word[0:3]) # Output: Pyt (from position 0 to 2) 

Finding the length of a string

The len() function tells you how many characters are in a string.

 phrase = "Python is awesome!" print(len(phrase)) # Output: 18 

Common errors

  1. Index Out of Range: If you try to access a position that doesn’t exist, Python will give an error.
 word = "Python" print(word[10]) # Error: IndexError: string index out of range 
  1. Unmatched Quotes: Make sure your string starts and ends with the same type of quotes.
message = "This will cause an error' # Mismatched quotes! 

Conclusion

Strings are essential for working with text in Python. You’ve learned:

  • What strings are and how to create them.
  • How to combine and repeat them.
  • How to access and manipulate parts of strings.
  • How to avoid common mistakes.

Strings are the building blocks for handling text in Python, so mastering them will help you unlock more advanced programming tasks in the future. Keep practicing!

Hands-On Practise

Exercise: Exploring strings

Write a Python program that does the following:

  1. Create a string variable called my_string and set it to "Python Programming is Fun!".
  2. Print the length of the string using the len() function.
  3. Access and print the first character of the string.
  4. Access and print the last character of the string (use negative indexing).
  5. Slice and print the word "Programming" from the string.
  6. Concatenate the string " Let's learn more!" to my_string and print the result.
  7. Repeat the string "Fun! " three times and print it.
Output:

Quizzes: 0/5

Question 1:

Which function is used to find the length of a string in Python?

  • length()
  • len()
  • size()
  • count()

Question 2:

What is the output of the following code? `python my_string = 'Python' print(my_string[0]) `

  • P
  • y
  • Python
  • IndexError

Question 3:

Which of the following is used to create a multi-line string in Python?

  • Single quotes ('')
  • Double quotes ("")
  • Triple quotes (''' or """)
  • Parentheses ()

Question 4:

What happens if you try to access an index in a string that doesn't exist?

  • It returns an empty string.
  • It raises an IndexError.
  • It returns None.
  • It wraps around to the first character.

Question 5:

What does the following code output? `python print('Hello' + ' ' + 'World!') `

  • HelloWorld!
  • Hello World!
  • Hello+ World!
  • SyntaxError

Feedback

Share your thoughts about this lesson

Discussion

Please log in to create a discussion.
Previous lessonLoops: Automating repetitionNext lessonPython List