Introduction:

Strings are an essential part of any programming language, and in Python, they are sequences of characters used to store text. You can perform various operations on strings, such as finding their length, converting between uppercase and lowercase, combining them, or even converting characters to their ASCII values and vice versa. These concepts will help you manipulate text in your programs, whether you’re working on a chatbot or a data analysis project.

Let’s explore some basic string handling techniques in Python, including:

  • len() – to find the length of a string
  • .upper() and .lower() – for converting the case of letters
  • String concatenation using the + operator
  • Converting between characters and ASCII codes using ord() and chr()

Run this code and observe how these methods work in action:

  • The len() function gives the length of the string.
  • .upper() and .lower() change the case of the text.
  • The + operator combines two strings.
  • The ord() function converts a character to its ASCII value, while chr() converts a number to the corresponding character.

Predict:

Can you predict the output of the following code?

Think about how the length of the string is calculated and what happens when you convert it to lowercase.

greeting = "Good evening!"
print(len(greeting))
print(greeting.lower()) 

Run:

Now it’s your turn to see how string handling works! Run this code in the embedded IDE below and see the results:

Investigate:

Let’s dive a little deeper. The following code introduces string concatenation and ASCII conversion. Run it and observe how Python joins strings and works with ASCII values.

Instructions:

  • Investigate how + is used to join the first and last name into one string.
  • Try changing the character 'A' to another letter and observe how the ASCII value changes. What happens if you change 65 to a different number?

Modify:

The following code has a small issue. Your task is to fix it! The code should display the correct full name and convert characters to their ASCII values, but there's a mistake in how the strings are concatenated. Can you spot the issue?

Hint: You need to add a space between the first and last names during concatenation.

Scroll to Top