Open In Colab

6. String#

Python strings are sequences of characters enclosed in quotes (either single quotes ‘ or double quotes “). They are used to represent text data in Python programs. Strings are an example of a built-in data type in Python.

Here’s a brief explanation of Python strings and some simple examples:

  1. Creating strings: Strings can be created using single quotes or double quotes. You can also use triple quotes (‘’’ or “””) to create multiline strings.

# Creating strings
string1 = 'Hello, World!'
string2 = "Python is fun!"
string3 = '''This is a
multiline
string.'''
  1. Accessing characters: You can access individual characters in a string using indexing, with the index starting from 0.

name = 'Alice'
first_char = name[0]  # 'A'
second_char = name[1]  # 'l'
  1. String slicing: You can extract a substring from a string using slicing. The syntax for slicing is string[start:stop:step], where start is the starting index, stop is the ending index (exclusive), and step is the interval between characters.

text = 'Python strings'
substring = text[0:6]  # 'Python'
  1. String length: You can find the length of a string using the built-in len() function.

message = 'Hello, World!'
length = len(message)  # 13
  1. String concatenation: You can join two strings together using the + operator.

greeting = 'Hello'
name = 'Alice'
message = greeting + ', ' + name  # 'Hello, Alice'
  1. String methods: Strings have many built-in methods that you can use to perform various operations, such as converting to uppercase, replacing characters, splitting strings, etc.

text = 'python programming'

# Convert to uppercase
uppercase_text = text.upper()  # 'PYTHON PROGRAMMING'

# Replace characters
replaced_text = text.replace('python', 'Java')  # 'Java programming'

# Split string
words = text.split(' ')  # ['python', 'programming']

Problem 1:#

You are given a list of dictionaries, each containing a user’s information: first name, last name, age, and country. Your task is to write a Python script that iterates through the list and prints out the user’s information in a formatted string as follows:

<First Name> <Last Name> is <Age> years old and comes from <Country>.
  Cell In[7], line 1
    <First Name> <Last Name> is <Age> years old and comes from <Country>.
    ^
SyntaxError: invalid syntax

Here’s the list of dictionaries:

users = [
    {'first_name': 'John', 'last_name': 'Doe', 'age': 30, 'country': 'USA'},
    {'first_name': 'Jane', 'last_name': 'Doe', 'age': 28, 'country': 'Canada'},
    {'first_name': 'Alice', 'last_name': 'Johnson', 'age': 25, 'country': 'UK'},
    {'first_name': 'Bob', 'last_name': 'Smith', 'age': 32, 'country': 'Australia'}
]

Output:#

John Doe is 30 years old and comes from USA.
Jane Doe is 28 years old and comes from Canada.
Alice Johnson is 25 years old and comes from UK.
Bob Smith is 32 years old and comes from Australia.

Problem 2:#

You are given a list of products, where each product is represented as a tuple containing the product’s name, price, and quantity. Your task is to write a Python script that iterates through the list and prints out the product information in a formatted string as follows:

<Product Name> - Price: $<Price> - Quantity: <Quantity>

Here’s the list of products:

products = [
    ('Laptop', 999.99, 5),
    ('Smartphone', 799.99, 10),
    ('Tablet', 399.99, 7),
    ('Headphones', 149.99, 25)
]

Output:#

Laptop - Price: $999.99 - Quantity: 5
Smartphone - Price: $799.99 - Quantity: 10
Tablet - Price: $399.99 - Quantity: 7
Headphones - Price: $149.99 - Quantity: 25

Problem 3:#

You are given a list of students, where each student is represented as a dictionary containing the student’s name and a list of their test scores. Your task is to write a Python script that calculates the average score for each student and prints out the student’s name along with their average score, formatted as follows:

<Student Name>: Average Score: <Average Score>

Here’s the list of students:

students = [
    {'name': 'John', 'scores': [85, 90, 78, 92, 88]},
    {'name': 'Jane', 'scores': [80, 85, 91, 89, 93]},
    {'name': 'Alice', 'scores': [92, 96, 80, 81, 85]},
    {'name': 'Bob', 'scores': [82, 84, 90, 78, 79]}
]

Output:#

John: Average Score: 86.60
Jane: Average Score: 87.60
Alice: Average Score: 86.80
Bob: Average Score: 82.60