Operations on Numbers and Strings

Estimated reading: 1 minute 37 views

Operations on Numbers

Basic Arithmetic Operations

# Basic Arithmetic Operations
num1 = 10
num2 = 5

sum_result = num1 + num2
difference_result = num1 - num2
product_result = num1 * num2
quotient_result = num1 / num2

print("Sum:", sum_result)
print("Difference:", difference_result)
print("Product:", product_result)
print("Quotient:", quotient_result)

Operations on Strings

Concatenation

# String Concatenation
str1 = "Hello"
str2 = "World"

concatenated_str = str1 + " " + str2
print("Concatenated String:", concatenated_str)

String Repetition

# String Repetition
repeated_str = str1 * 3
print("Repeated String:", repeated_str)

String Length

# String Length
length = len(concatenated_str)
print("Length of String:", length)

Uppercase and Lowercase

# Uppercase and Lowercase
uppercase_str = concatenated_str.upper()
lowercase_str = concatenated_str.lower()

print("Uppercase String:", uppercase_str)
print("Lowercase String:", lowercase_str)

Type Conversion between Numbers and Strings

# Type Conversion
num_str = "25"
str_to_num = int(num_str)

num = 42
num_to_str = str(num)

print("String to Number:", str_to_num)
print("Number to String:", num_to_str)

Additional Resources

Share this Doc

Operations on Numbers and Strings

Or copy link