Using len for text but discarding spaces in the count
Asked Answered
C

10

9

So, I am trying to create a program which counts the number of characters in a string which the user inputs, but I want to discard any spaces that the user enters.

def main():
    full_name = str(input("Please enter in a full name: ")).split(" ")

    for x in full_name:
        print(len(x))


main()

Using this, I can get the number of the characters in each word, without spaces, but I don't know how to add each number together and print the total.

Compile answered 29/10, 2013 at 20:59 Comment(2)
Why are you splitting the phrase?Robison
I thought I would split them and then count them up that way. This allows me to count it without the spaces, but I realize now that it's not very efficient to do that, lol.Compile
E
23

Count the length and subtract the number of spaces:

>>> full_name = input("Please enter in a full name: ")
Please enter in a full name: john smith
>>> len(full_name) - full_name.count(' ')
9
>>> len(full_name)
Efrenefron answered 29/10, 2013 at 21:2 Comment(2)
Brilliant! As you can see, I'm a beginner programmer. Thanks for the tips!Compile
@Izento: no sweat! Since you are new to SO, please consider acceptingEfrenefron
R
7

Use sum with a generator expression:

>>> text = 'foo  bar  spam'
>>> sum(len(x) for x in text.split())
10

Or str.translate with len:

>>> from string import whitespace
>>> len(text.translate(None, whitespace)) #Handles all types of whitespace characters
10
Rhyme answered 29/10, 2013 at 21:1 Comment(1)
This requires the creation of new lists with each splited chunk, which has O(n) space complexity. Would be better to subtract the number of spaces from the total length (no extra space + O(n) time). I like the translate idea, thoughEfrenefron
G
3

Why can't you just do:

>>> mystr = input("Please enter in a full name: ")
Please enter in a full name: iCodez wrote this
>>> len(mystr.replace(" ", ""))
15
>>> len(mystr)
17
>>>

This gets the length of the string minus the spaces.

Ginter answered 29/10, 2013 at 21:1 Comment(0)
E
1

I can propose a few versions.

You can replace each space with an empty string and calculate the length:

len(mystr.replace(" ", ""))

You can calculate the length of the whole string and subtract the number of spaces:

len(mystr) - mystr.count(' ')

Or you can sum the lengths of all substrings after splitting the string with spaces:

sum(map(len, mystr.split(' ')))
Escuage answered 29/10, 2013 at 21:3 Comment(0)
A
1

To count the number of characters excluding spaces, you can simply do:

>>> full_name = "John DOE"
>>> len(full_name) - full_name.count(' ')
7
Autocracy answered 29/10, 2013 at 21:8 Comment(0)
R
0

Some code as close as possible to your original:

def main():
    full_name = input("Please enter in a full name: ").split()
    total = 0
    for x in full_name:
        total += len(x)
    print(total)

However, I think len(full_name) - full_name.count(' ') is better.

Retroversion answered 29/10, 2013 at 21:8 Comment(0)
O
0

You can also do

sum(1 for c in s if c!=' ') 

Which avoids any unnecessary temporary string or list.

Oletta answered 29/10, 2013 at 21:13 Comment(0)
M
0

Explained in comments

string1 = input ()

# just when the condition is true add 1 
res = sum(1 for c in string1 if c!=' ')

# it's shorter and still works
res2 = sum(c!=' ' for c in string1)

# summing ones may be more explicit but it's unnecessary
print(res, res2) 
c v s
3 3

[Program finished]
Mol answered 7/3, 2021 at 10:10 Comment(0)
S
0

I tried this, it's a bit simple:

P = "What is going on "

S = " "

i = 0


while i < len(P) and P[i] != S:
    i += 1

print(len(P)-i)
Smirk answered 25/9 at 18:32 Comment(1)
Thank you for your interest in contributing to the Stack Overflow community. This question already has quite a few answers—including one that has been extensively validated by the community. Are you certain your approach hasn’t been given previously? If so, it would be useful to explain how your approach is different, under what circumstances your approach might be preferred, and/or why you think the previous answers aren’t sufficient. Can you kindly edit your answer to offer an explanation?Haye
C
-1

I also found that this works. I am a beginner programmer and I did not understand anything that was going on in the other explanations. I think that this might be a little easier to understand for a beginner beginner.

full_name = str(input("Please enter in your full name: "))
spaces = str(" ")
spaces_name = str(len(full_name) - len(spaces))

print("Your name " + full_name + " has " + spaces_name + "charictures")
Cybernetics answered 12/3 at 9:21 Comment(1)
Hi Caleb and thanks for joining and answering, except I'm afraid the answer is incorrect so I'm going to have to mark it down. Your 'spaces' is always going to be a single space and hence, the len(" ") is always going to be 1 so you're simply subtracting one from the length of the entered name. If the name entered is "Madonna" or "Lee Harvey Oswald" for example, it'll be wrong. As an aside, you don't need str(" ") The " " is already a string so it's just spaces = " ".Sherikasherill

© 2022 - 2024 — McMap. All rights reserved.