Programming Pandit

c/c++/c#/Javav/Python


Latest Update

Monday, March 11, 2024

python program to count the number of characters in a string.

Objective: Write a python program to count the number of characters in a string.


Code:

# Prompt user for input

input_string = input("Enter a string: ")


# Initialize a variable to count characters

char_count = 0


# Loop through each character in the string

for char in input_string:

    # Increment the character count for each valid character

    char_count += 1


# Print the total number of characters in the string

print("The number of characters in the string is:", char_count)

 

Output:




No comments:

Post a Comment