Ad Code

Responsive Advertisement

Basic Python Program - 41

Question 41: Write a Python program to generate a random float where the value is between 5 and 50 using Python math module.
Download hole Program / Project code, by clicking following link:
Give the syntax and example of title() and capitalize() methods ?
In Python, both the title() and capitalize() methods are used to manipulate strings by changing the capitalization of characters. However, they serve slightly different purposes:
Here's how the string formatting operator works:
  1. title() Method:
    • The title() method is used to capitalize the first letter of each word in a string.
    • It is commonly used to format strings as titles or headings.
    • It does not affect the rest of the letters in the words, which are converted to lowercase.

    Syntax of title() method:
    string.title()

    Example of title() method:
    text = "hello world"
    formatted_text = text.title()
    print(formatted_text)

    Output:
    Hello World

    In this example, the title() method capitalizes the first letter of each word in the string "hello world," resulting in "Hello World."
  2. capitalize() Method:
    • The capitalize() method is used to capitalize only the first character of a string.
    • It converts the first character to uppercase while leaving the rest of the string in lowercase.
    Syntax of capitalize() method:
    string.capitalize()

    Example of capitalize() method:
    text = "hello world"
    formatted_text = text.capitalize()
    print(formatted_text)

    Output:
    Hello world

    In this example, the capitalize() method capitalizes only the first letter of the string "hello world," resulting in "Hello world." The rest of the characters remain in lowercase.

    To summarize, title() is used to capitalize the first letter of each word in a string, while capitalize() is used to capitalize only the first character of a string. Choose the method that best suits your specific capitalization needs when working with strings in Python.
Programming Code:
Following code write in: BP_P41.py
# Generate random float where the value between 5 to 50

# use Python Math module

# import lib.
import random

print("The Random Float number is between 5 to 50: ",
        random.uniform(5, 50))

# use uniform() function

# Thanks for Reading.
Output:

Post a Comment

0 Comments

Ad Code

Responsive Advertisement