Question 40: Write a Python function that accepts a string and calculate the number of upper case letters and lower case letters. Download hole Program / Project code, by clicking following link: Describe about string formatting operator with example in Python ? In Python, string formatting is a way to create formatted strings by substituting values into a string template. The string formatting operator % is commonly used for this purpose, although there are newer methods using f-strings (introduced in Python 3.6) and the str.format() method.
Here's how the string formatting operator works: - String Template: You start with a string that serves as a template, containing placeholders for values you want to insert. These placeholders are represented by % followed by a format specifier.
- Values to Insert: You provide the values you want to insert into the template. These values are typically provided as a tuple or a dictionary.
- Formatting: You use the % operator to format the template string with the values. The format specifiers in the template determine how the values are formatted in the resulting string.
Here's an example of using the string formatting operator %:
name = "Alice"
age = 30
# Create a formatted string using the % operator
message = "Hello, %s! You are %d years old." % (name, age)
print(message)
Output:
Hello, Alice! You are 30 years old.
In this example: - %s is a placeholder for a string.
- %d is a placeholder for an integer.
The % operator is followed by a tuple (name, age), which contains the values to be inserted into the string. The operator replaces %s with the value of name (which is a string) and %d with the value of age (which is an integer).
You can use different format specifiers based on the type of data you are inserting into the string, such as %s for strings, %d for integers, %f for floating-point numbers, and so on. The % operator provides flexible string formatting options.
However, note that f-strings and the str.format() method are more modern and preferred ways of achieving string formatting in Python, especially in Python 3.6 and later versions.
Here's the same example using an f-string:
name = "Alice"
age = 30
# Create a formatted string using an f-string
message = f"Hello, {name}! You are {age} years old."
print(message)
The output will be the same:
Hello, Alice! You are 30 years old.
F-strings and str.format() provide more flexibility and readability compared to the older % operator, especially when dealing with complex formatting requirements.
Programming Code: Following code write in: BP_P40.py # Python function that accepts a string
# also calculate number of upper and lower case letter of String
def string_cal(str1):
d = {"Upper_Case": 0, "Lower_Case": 0}
for i in str1:
if i.isupper(): # isupper() function
d["Upper_Case"] += 1
elif i.islower(): # islower() function
d["Lower_Case"] += 1
else:
pass
print("Number of Upper Case letters: ", d["Upper_Case"])
print("Number of Lower Case letters: ", d["Lower_Case"])
str1 = input("Enter String that contains Upper & Lower case Letters: ")
print("The String is: ", str1)
string_cal(str1)
# Thanks for Reading.
Output:
Describe about string formatting operator with example in Python ?
In Python, string formatting is a way to create formatted strings by substituting values into a string template. The string formatting operator % is commonly used for this purpose, although there are newer methods using f-strings (introduced in Python 3.6) and the str.format() method.
Here's how the string formatting operator works:
Here's how the string formatting operator works:
- String Template: You start with a string that serves as a template, containing placeholders for values you want to insert. These placeholders are represented by % followed by a format specifier.
- Values to Insert: You provide the values you want to insert into the template. These values are typically provided as a tuple or a dictionary.
- Formatting: You use the % operator to format the template string with the values. The format specifiers in the template determine how the values are formatted in the resulting string.
Here's an example of using the string formatting operator %:
name = "Alice"
age = 30
# Create a formatted string using the % operator
message = "Hello, %s! You are %d years old." % (name, age)
print(message)
Output:
Hello, Alice! You are 30 years old.
In this example:
name = "Alice"
age = 30
# Create a formatted string using the % operator
message = "Hello, %s! You are %d years old." % (name, age)
print(message)
Output:
Hello, Alice! You are 30 years old.
In this example:
- %s is a placeholder for a string.
- %d is a placeholder for an integer.
You can use different format specifiers based on the type of data you are inserting into the string, such as %s for strings, %d for integers, %f for floating-point numbers, and so on. The % operator provides flexible string formatting options.
However, note that f-strings and the str.format() method are more modern and preferred ways of achieving string formatting in Python, especially in Python 3.6 and later versions.
Here's the same example using an f-string:
name = "Alice"
age = 30
# Create a formatted string using an f-string
message = f"Hello, {name}! You are {age} years old."
print(message)
The output will be the same:
Hello, Alice! You are 30 years old.
name = "Alice"
age = 30
# Create a formatted string using an f-string
message = f"Hello, {name}! You are {age} years old."
print(message)
The output will be the same:
Hello, Alice! You are 30 years old.
F-strings and str.format() provide more flexibility and readability compared to the older % operator, especially when dealing with complex formatting requirements.
# Python function that accepts a string
# also calculate number of upper and lower case letter of String
def string_cal(str1):
d = {"Upper_Case": 0, "Lower_Case": 0}
for i in str1:
if i.isupper(): # isupper() function
d["Upper_Case"] += 1
elif i.islower(): # islower() function
d["Lower_Case"] += 1
else:
pass
print("Number of Upper Case letters: ", d["Upper_Case"])
print("Number of Lower Case letters: ", d["Lower_Case"])
str1 = input("Enter String that contains Upper & Lower case Letters: ")
print("The String is: ", str1)
string_cal(str1)
# Thanks for Reading.
Output:
0 Comments