String

Python program to count the total number of characters in the given string

Python program to count the total number of characters in the given string

In this article, we will discuss the concept of Python program to count the total number of characters in the given string

In this post, we are going to learn how to count the total number of characters in the given string in Python  programming language

Total number of characters

Python program to count the total number of characters using for loop

The program allows the user to enter a string and thereafter It counts the total characters of the string without space using for loop in Python language

Program 1

#Python program to count total characters of a given string
str1=raw_input("Please enter your a string as you wish: ")
count=0

for i in range(len(str1)):
    if(str1[i]!=' '):#avoid counting the space using this condition
        count=count+1
print("Total number of characters in this string: ",str(count));

When the above code is executed, it produces the following result

Please enter a string as you wish: python language
Total number of characters in this string: 14

 

Program 2

#python program to count total number of character in a string
str1=input("Enter a string as you wish \n");
total=0
for i in range(len(str1)):
  if(str1[i]!=' '):
    total=total+1
print("Total number of character in this string = ",total)

When the above code is executed, it produces the following result

Enter a string as you wish
python language
Total number of character in this string = 14

 

Approach

  1. Declare and initialize an integer variable as total=0;
  2. the user asked to enter a string to count character
  3. A for-loop is used to count total characters of the given string using the total variable
  4. Use an if condition to test if(str.charAt(i)!=’ ‘), if it is true,  total  becomes total + 1(total=total+1)
  5. Finally, the program displays the total number of the character using the total variable

 

Python program to count the total number of characters using while loop

The program allows the user to enter a string and thereafter It counts the total characters of the string without space using while loop in Python language

Program 3

#python program to count total character of a String 
str1=input("Input a string as you wish: \n")
total=0
i=0
while(i<len(str1)):
  if(str1[i]!=' '):#avoid counting the space using this condition
   total=total+1
  i=i+1
print("total number of character in this string is: ", total)

When the above code is executed, it produces the following result

Input a string as you wish:
python programming language
total number of character in this string is: 25

 

Approach

  1. Declare and initialize an integer variables as total=0,i=0;
  2. The user asked to enter a string to count character
  3. A while-loop is used to count total characters of the given string using the total variable
  4. Use an if condition to test if(str.charAt(i)!=’ ‘), if it is true,  total  becomes total + 1(total=total+1)
  5. Finally, the program displays the total number of the character using the total variable

 

Suggested for you

For loop in Python language

While loop in Python language

 

Similar post

Java program to count the total number of characters in the given string

C++ program to count the total number of characters in the given string

C program to count the total number of characters in the given string

 

C++ program to count the total number of characters in the given string
Java program to count the total words in the given string
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

C# inverted full pyramid star pattern

C# inverted full pyramid star pattern In this article, we will discuss the concept of…

3 weeks ago

C# Full Pyramid star pattern program

C# Full Pyramid star pattern program In this article, we will discuss the concept of…

1 month ago

Program to count vowels,consonants,words, characters and space in Java

Program to count vowels, consonants, words, characters and space in Java In this article, we…

1 month ago

How to print multiplication table using Array in C++ language

How to print multiplication table using Array in C++ language In this post, we will…

1 month ago

C Program to multiplication table using Array

C Program to multiplication table using Array In this tutorial , we will discuss about…

2 months ago

Java program to check odd or even using recursion

Java program to check odd or even using recursion In this tutorial, we discuss a…

2 months ago