Exercise Programs
1. Define variables with different data types and print using
different methods.
Input:
# Define variables with different data types
integer_var = 10 # Integer
float_var = 20.5 # Float
string_var = "Hello, World!" # String
boolean_var = True # Boolean
list_var = [1, 2, 3, 4, 5] # List
tuple_var = (6, 7, 8) # Tuple
dict_var = {'key1': 'value1', 'key2': 'value2'} # Dictionary
none_var = None # NoneType
# Print using different methods
# Method 1: Using print()
print(integer_var)
print(float_var)
print(string_var)
print(boolean_var)
print(list_var)
print(tuple_var)
print(dict_var)
print(none_var)
# Method 2: Using formatted strings (f-strings)
print(f"Integer: {integer_var}")
print(f"Float: {float_var}")
print(f"String: {string_var}")
print(f"Boolean: {boolean_var}")
print(f"List: {list_var}")
print(f"Tuple: {tuple_var}")
print(f"Dictionary: {dict_var}")
print(f"None: {none_var}")
# Method 3: Using .format() method
print("Integer: {}".format(integer_var))
print("Float: {}".format(float_var))
print("String: {}".format(string_var))
print("Boolean: {}".format(boolean_var))
print("List: {}".format(list_var))
print("Tuple: {}".format(tuple_var))
print("Dictionary: {}".format(dict_var))
print("None: {}".format(none_var))
# Method 4: Using % formatting
print("Integer: %d" % integer_var)
print("Float: %.1f" % float_var)
print("String: %s" % string_var)
print("Boolean: %s" % boolean_var)
print("List: %s" % list_var)
print("Tuple: %s" % (tuple_var,))
print("Dictionary: %s" % dict_var)
print("None: %s" % none_var)
Output:
10
20.5
Hello, World!
True
[1, 2, 3, 4, 5]
(6, 7, 8)
{'key1': 'value1', 'key2': 'value2'}
None
Integer: 10
Float: 20.5
String: Hello, World!
Boolean: True
List: [1, 2, 3, 4, 5]
Tuple: (6, 7, 8)
Dictionary: {'key1': 'value1', 'key2': 'value2'}
None: None
Integer: 10
Float: 20.5
String: Hello, World!
Boolean: True
List: [1, 2, 3, 4, 5]
Tuple: (6, 7, 8)
Dictionary: {'key1': 'value1', 'key2': 'value2'}
None: None
Integer: 10
Float: 20.5
String: Hello, World!
Boolean: True
List: [1, 2, 3, 4, 5]
Tuple: (6, 7, 8)
Dictionary: {'key1': 'value1', 'key2': 'value2'}
None: None
2. Convert data to other type and print it.
Input:
# Define a variable with an integer type
value = 42 # Integer
# Convert the variable to other data types and print the results
# Integer to float
float_value = float(value)
print(f"Integer to Float: {float_value}")
# Integer to string
string_value = str(value)
print(f"Integer to String: {string_value}")
# Integer to boolean (0 is False, any other integer is True)
boolean_value = bool(value)
print(f"Integer to Boolean: {boolean_value}")
# Integer to complex number
complex_value = complex(value)
print(f"Integer to Complex: {complex_value}")
# Integer to hexadecimal string
hex_value = hex(value)
print(f"Integer to Hexadecimal: {hex_value}")
# Integer to binary string
binary_value = bin(value)
print(f"Integer to Binary: {binary_value}")
# Integer to octal string
octal_value = oct(value)
print(f"Integer to Octal: {octal_value}")
Output:
Integer to Float: 42.0
Integer to String: 42
Integer to Boolean: True
Integer to Complex: (42+0j)
Integer to Hexadecimal: 0x2a
Integer to Binary: 0b101010
Integer to Octal: 0o52
3. Take input from the user his/her first name and last name and
display full name.
Input:
# Take input from the user for first name and last name
first_name = input("Enter your first name: ")
last_name = input("Enter your last name: ")
# Concatenate first name and last name to form the full name
full_name = first_name + " " + last_name
# Display the full name
print("Your full name is:", full_name)
Output:
Enter your first name: Priyank
Enter your last name: Vagadiya
Your full name is: Priyank Vagadiya
4. Take string from the user and find the length of the string .
Convert into all lower case and uppercase , first letter capital and
display the string.
Input:
# Take a string input from the user
user_string = input("Enter a string: ")
# Find the length of the string
string_length = len(user_string)
# Convert the string to all lowercase
lowercase_string = user_string.lower()
# Convert the string to all uppercase
uppercase_string = user_string.upper()
# Capitalize the first letter of the string
capitalized_string = user_string.capitalize()
# Display the results
print(f"Length of the string: {string_length}")
print(f"String in lowercase: {lowercase_string}")
print(f"String in uppercase: {uppercase_string}")
print(f"String with the first letter capitalized: {capitalized_string}")
Output:
Enter a string: My Name Is Priyank
Length of the string: 18
String in lowercase: my name is priyank
String in uppercase: MY NAME IS PRIYANK
String with the first letter capitalized: My name is priyank
[Link] string from the user and display only first three character of
it.
Input:
# Take a string input from the user
user_string = input("Enter a string: ")
# Display only the first three characters
# If the string is less than three characters, it will display the entire string
first_three_chars = user_string[:3]
print(f"The first three characters of the string are: {first_three_chars}")
Output:
Enter a string: My Name Is Priyank
The first three characters of the string are: My
[Link] string from the user and display last three character of it.
Input:
# Take a string input from the user
user_string = input("Enter a string: ")
# Display only the last three characters
# If the string is less than three characters, it will display the entire string
last_three_chars = user_string[-3:]
print(f"The last three characters of the string are: {last_three_chars}")
Output:
Enter a string: My Name Is Priyank
The last three characters of the string are: ank
7. Split the string.
Input:
# Take a string input from the user
user_string = input("Enter a string: ")
# Split the string into a list of words
split_string = user_string.split()
# Display the split string
print(f"The split string is: {split_string}")
Output:
Enter a string: My Name Is Priyank
The split string is: ['My', 'Name', 'Is', 'Priyank']
8. Input the string and character from the user and find how many
times a particular character exists in the string.
Input:
# Take a string input from the user
user_string = input("Enter a string: ")
# Take a character input from the user
char_to_find = input("Enter a character to find: ")
# Check if the input is a single character
if len(char_to_find) != 1:
print("Please enter a single character.")
else:
# Count how many times the character exists in the string
char_count = user_string.count(char_to_find)
# Display the count
print(f"The character '{char_to_find}' appears {char_count} time(s) in the
string.")
Output:
Enter a string: My Name Is Priyank
Enter a character to find: P
The character 'P' appears 1 time(s) in the string.
9. Find the particular character in the string and replace with a
space.
Input:
# Take a string input from the user
user_string = input("Enter a string: ")
# Take a character input from the user
char_to_replace = input("Enter a character to replace with a space: ")
# Check if the input is a single character
if len(char_to_replace) != 1:
print("Please enter a single character.")
else:
# Replace the specified character with a space
modified_string = user_string.replace(char_to_replace, ' ')
# Display the modified string
print(f"The modified string is: {modified_string}")
Output:
Enter a string: My Name Is Priyank
Enter a character to replace with a space: I
The modified string is: My Name s Priyank