8/25/23, 3:54 PM Week 2(Class#3 Assignment # 2)(String concatination & Formating ).
ipynb - Colaboratory
Topic: String Concatinating by diffrent mothods
Assignment:2
Name: Raheel Nadeem (Data Science & AI student,NEDUET)
Methods used for String Concatination
Using the + operator
Using the `.format()
Using f'' string interpolation
Using %() string formatting
Case 1: String Concatenation and Formatting
Instructions:
1. Create a variable name and assign your name to it.
2. Create a variable age and assign your age to it.
3. Create a variable country and assign your country of residence to it.
4. Using the + operator, concatenate the variables to form a sentence in the following format: "My name is <name> , I am <age> years old,
and I live in <country> ."
5. Print the concatenated sentence.
name = "Raheel Nadeem"
age = "29"
country = "Pakistan"
data1 = "My name is " + name + " I am "+age+" years old " + "and i lives in " +country
data2 = "My name is " + name + " \nI am "+age+" years old " + " \nand i lives in " +country
print (data1)
print (data2)
My name is Raheel Nadeem I am 29 years old and i lives in Pakistan
My name is Raheel Nadeem
I am 29 years old
and i lives in Pakistan
Case 2: String Formatting using .format()
Instructions:
1. Create a variable item and assign a product name to it (e.g., "laptop", "book", "phone").
2. Create a variable price and assign a price to the item.
3. Create a variable quantity and assign a quantity value.
4. Calculate the total cost using the formula: total_cost = price * quantity .
5. Using the .format() method, create a formatted string: "I bought <quantity> <item> (s) at <price> each, for a total of $ <total_cost> ."
6. Print the formatted string.
price_int = 100
quantity_int = 25
item = "laptop"
price = "100$"
quantity = "25"
total_cost = int(price_int * quantity_int)
data = """i bought {} {} at {} each for of total of $ {}""".format(quantity,item,price,total_cost)
print (data)
i bought 25 laptop at 100$ each for of total of $ 2500
item = "laptop"
price = 100
[Link] 1/3
8/25/23, 3:54 PM Week 2(Class#3 Assignment # 2)(String concatination & Formating ).ipynb - Colaboratory
quantity = 25
total_cost = int(price_int * quantity_int)
data = """i bought {} {} at {} each for of total of $ {}""".format(quantity,item,price,total_cost)
print (data)
i bought 25 laptop at 100 each for of total of $ 2500
ACase 3: String Interpolation using f''
Instructions:
1. Create a variable city and assign the name of a city.
2. Create a variable temperature and assign a temperature value in Celsius.
3. Using f'' string interpolation, create a string: "The temperature in <city> is <temperature> °C."
4. Print the interpolated string
city = "Karachi"
temperature = "30 °C"
data = f"""The temperature in {city} is {temperature}"""
print (data)
The temperature in Karachi is 30 °C
Case 4: String Formatting using %() *
Instructions:
1. Create a variable first_name and assign your first name to it.
2. Create a variable last_name and assign your last name to it.
3. Create a variable birth_year and assign your birth year.
4. Calculate your age using the formula: age = current_year - birth_year , where current_year is the current year.
5. Using %() string formatting, create a string: "My name is %(first_name)s %(last_name)s. I am %(age)d years old."
6. Print the formatted string.
first_name = "Raheel"
last_name = "Nadeem"
birth_year = 1994
current_year = 2023
age = current_year - birth_year
"My name is %s %s.I am %d years old" %(first_name,last_name,age)
'My name is Raheel Nadeem.I am 29 years old'
Case 5: Combining Formatting Methods
Instructions:
1. Create a variable product and assign a product name.
2. Create a variable discount and assign a discount percentage.
3. Create a variable original_price and assign an original price.
4. Calculate the discounted price using the formula: discounted_price = original_price * (1 - discount / 100) .
5. Using any combination of string formatting methods ( + , .format() , f'' , %() ), create a descriptive string with the product name, original
price, discount percentage, and discounted price.
6. Print the descriptive string.
product = "9.5 mm Deformed steel bar"
discount = "10%"
orignal_price = 275000
discounted_price_calculation = orignal_price * (1-10/100)
discount_price = discounted_price_calculation
data = f"""Hello customers i am selling {product} at orignal factory price that is, {discounted_price_calculation}\
\nthe orignal price of this product is {orignal_price} and i am selling it with 10% discount"""
print (data)
Hello customers i am selling 9.5 mm Deformed steel bar at orignal factory price that is, 247500.0
the orignal price of this product is 275000 and i am selling it with 10% discount
[Link] 2/3
8/25/23, 3:54 PM Week 2(Class#3 Assignment # 2)(String concatination & Formating ).ipynb - Colaboratory
Colab paid products - Cancel contracts here
[Link] 3/3