# Number of Segments in a String using Python

The problem of counting the number of segments in a string is a popular coding interview question. Here you will be given a string, and you have to count the number of non-space characters in the string. So, if you want to know how to solve this problem, this article is for you. In this article, I will take you through how to count the number of segments in a string using Python.

## Number of Segments in a String

In the problem of counting the number of segments in a string, you will get a phrase or character in a string. To solve this problem, you need to find the total number of characters in the string, excluding the spaces. For example, look at the input and output of this problem shown below:

• Input: “Hey, my Instagram username is amankharwal.official” | Output: 6

The output of the string in the above example is 6 because there are six characters in the string.

## Number of Segments in a String using Python

I hope you have now understood what the problem of counting the number of segments in a string means. Below is how you can solve this problem using the Python programming language:

```def countSegments(s):
count = len(s.split())
return count

print(countSegments("Hey, my instagram username is amankharwal.official"))```
`Output: 6`

So this is how you can count the number of counting the number of segments in a string using Python. You can find many more practice questions for coding interviews solved and explained using Python here.

### Summary

In the problem of counting the number of segments in a string, you will get a phrase or character in a string. To solve this problem, you need to find the total number of characters in the string, excluding the spaces. I hope you liked this article on counting the number of segments in a string using Python. Feel free to ask valuable questions in the comments section below. ##### Aman Kharwal

I'm a writer and data scientist on a mission to educate others about the incredible power of data📈.

Articles: 1498