calculate punctuation percentage in a string in Python
I have been working on calculating the percentage of punctuations in a sentence. For some reason, my function works when doing double spacing, but counts all the characters and the white space. For example, I have a text DEACTIVATE: OK
so total full length is 14 when I subtract the punctuation then length is 13, so percentage should be 1/13 = 7.63%
, however, my function gives me 7.14%, which is basically 1/14 = 7.14%
.
On the other side, if have just one white space, my function throws me an error
"ZeroDivisionError: division by zero".
Here is my code for your reference and a simple text samples
text= "Centre to position, remaining shift is still larger than maximum (retry nbr=1, centring_stroke.r=2.7662e-05, max centring stroke.r=2.5e-05)"
text2= "DEACTIVATE: KU-1421"
import string
def count_punct(text):
count = sum([1 for char in text if char in string.punctuation])
return round(count/(len(text) - text.count(" ")), 3)*100
df_sub['punct%'] = df_sub['Err_Text2'].apply(lambda x: count_punct(x))
df_sub.head(20)
python percentage punctuation
|
show 4 more comments
I have been working on calculating the percentage of punctuations in a sentence. For some reason, my function works when doing double spacing, but counts all the characters and the white space. For example, I have a text DEACTIVATE: OK
so total full length is 14 when I subtract the punctuation then length is 13, so percentage should be 1/13 = 7.63%
, however, my function gives me 7.14%, which is basically 1/14 = 7.14%
.
On the other side, if have just one white space, my function throws me an error
"ZeroDivisionError: division by zero".
Here is my code for your reference and a simple text samples
text= "Centre to position, remaining shift is still larger than maximum (retry nbr=1, centring_stroke.r=2.7662e-05, max centring stroke.r=2.5e-05)"
text2= "DEACTIVATE: KU-1421"
import string
def count_punct(text):
count = sum([1 for char in text if char in string.punctuation])
return round(count/(len(text) - text.count(" ")), 3)*100
df_sub['punct%'] = df_sub['Err_Text2'].apply(lambda x: count_punct(x))
df_sub.head(20)
python percentage punctuation
The code above is with two spaces (text.count(" ")). The code I am getting error is with single space (text.count(" ")).
– Vishwas
Nov 14 '18 at 5:39
That's not how percentage works. You don't subtract the number of punctuation characters, it's just(number of punctuation characters) / (length of string)
, unless you mean subtract the whitespace, not the punctuation.
– Tomothy32
Nov 14 '18 at 5:41
you mean like this "return round((count)/(len(text) - text.count(" ")), 3)*100"? I still get same error.
– Vishwas
Nov 14 '18 at 5:43
Your error is division by zero? That means you passed in some string that is either fully spaces or empty.
– Tomothy32
Nov 14 '18 at 5:47
Thats what I am not able to figure out. Any suggestion or advice will be much appreciated.
– Vishwas
Nov 14 '18 at 5:50
|
show 4 more comments
I have been working on calculating the percentage of punctuations in a sentence. For some reason, my function works when doing double spacing, but counts all the characters and the white space. For example, I have a text DEACTIVATE: OK
so total full length is 14 when I subtract the punctuation then length is 13, so percentage should be 1/13 = 7.63%
, however, my function gives me 7.14%, which is basically 1/14 = 7.14%
.
On the other side, if have just one white space, my function throws me an error
"ZeroDivisionError: division by zero".
Here is my code for your reference and a simple text samples
text= "Centre to position, remaining shift is still larger than maximum (retry nbr=1, centring_stroke.r=2.7662e-05, max centring stroke.r=2.5e-05)"
text2= "DEACTIVATE: KU-1421"
import string
def count_punct(text):
count = sum([1 for char in text if char in string.punctuation])
return round(count/(len(text) - text.count(" ")), 3)*100
df_sub['punct%'] = df_sub['Err_Text2'].apply(lambda x: count_punct(x))
df_sub.head(20)
python percentage punctuation
I have been working on calculating the percentage of punctuations in a sentence. For some reason, my function works when doing double spacing, but counts all the characters and the white space. For example, I have a text DEACTIVATE: OK
so total full length is 14 when I subtract the punctuation then length is 13, so percentage should be 1/13 = 7.63%
, however, my function gives me 7.14%, which is basically 1/14 = 7.14%
.
On the other side, if have just one white space, my function throws me an error
"ZeroDivisionError: division by zero".
Here is my code for your reference and a simple text samples
text= "Centre to position, remaining shift is still larger than maximum (retry nbr=1, centring_stroke.r=2.7662e-05, max centring stroke.r=2.5e-05)"
text2= "DEACTIVATE: KU-1421"
import string
def count_punct(text):
count = sum([1 for char in text if char in string.punctuation])
return round(count/(len(text) - text.count(" ")), 3)*100
df_sub['punct%'] = df_sub['Err_Text2'].apply(lambda x: count_punct(x))
df_sub.head(20)
python percentage punctuation
python percentage punctuation
edited Nov 14 '18 at 11:15
Vineeth Sai
2,48851323
2,48851323
asked Nov 14 '18 at 5:35
VishwasVishwas
177
177
The code above is with two spaces (text.count(" ")). The code I am getting error is with single space (text.count(" ")).
– Vishwas
Nov 14 '18 at 5:39
That's not how percentage works. You don't subtract the number of punctuation characters, it's just(number of punctuation characters) / (length of string)
, unless you mean subtract the whitespace, not the punctuation.
– Tomothy32
Nov 14 '18 at 5:41
you mean like this "return round((count)/(len(text) - text.count(" ")), 3)*100"? I still get same error.
– Vishwas
Nov 14 '18 at 5:43
Your error is division by zero? That means you passed in some string that is either fully spaces or empty.
– Tomothy32
Nov 14 '18 at 5:47
Thats what I am not able to figure out. Any suggestion or advice will be much appreciated.
– Vishwas
Nov 14 '18 at 5:50
|
show 4 more comments
The code above is with two spaces (text.count(" ")). The code I am getting error is with single space (text.count(" ")).
– Vishwas
Nov 14 '18 at 5:39
That's not how percentage works. You don't subtract the number of punctuation characters, it's just(number of punctuation characters) / (length of string)
, unless you mean subtract the whitespace, not the punctuation.
– Tomothy32
Nov 14 '18 at 5:41
you mean like this "return round((count)/(len(text) - text.count(" ")), 3)*100"? I still get same error.
– Vishwas
Nov 14 '18 at 5:43
Your error is division by zero? That means you passed in some string that is either fully spaces or empty.
– Tomothy32
Nov 14 '18 at 5:47
Thats what I am not able to figure out. Any suggestion or advice will be much appreciated.
– Vishwas
Nov 14 '18 at 5:50
The code above is with two spaces (text.count(" ")). The code I am getting error is with single space (text.count(" ")).
– Vishwas
Nov 14 '18 at 5:39
The code above is with two spaces (text.count(" ")). The code I am getting error is with single space (text.count(" ")).
– Vishwas
Nov 14 '18 at 5:39
That's not how percentage works. You don't subtract the number of punctuation characters, it's just
(number of punctuation characters) / (length of string)
, unless you mean subtract the whitespace, not the punctuation.– Tomothy32
Nov 14 '18 at 5:41
That's not how percentage works. You don't subtract the number of punctuation characters, it's just
(number of punctuation characters) / (length of string)
, unless you mean subtract the whitespace, not the punctuation.– Tomothy32
Nov 14 '18 at 5:41
you mean like this "return round((count)/(len(text) - text.count(" ")), 3)*100"? I still get same error.
– Vishwas
Nov 14 '18 at 5:43
you mean like this "return round((count)/(len(text) - text.count(" ")), 3)*100"? I still get same error.
– Vishwas
Nov 14 '18 at 5:43
Your error is division by zero? That means you passed in some string that is either fully spaces or empty.
– Tomothy32
Nov 14 '18 at 5:47
Your error is division by zero? That means you passed in some string that is either fully spaces or empty.
– Tomothy32
Nov 14 '18 at 5:47
Thats what I am not able to figure out. Any suggestion or advice will be much appreciated.
– Vishwas
Nov 14 '18 at 5:50
Thats what I am not able to figure out. Any suggestion or advice will be much appreciated.
– Vishwas
Nov 14 '18 at 5:50
|
show 4 more comments
1 Answer
1
active
oldest
votes
Here, Make these small changes and your count_punct
function should be up and running.. The reason your code was breaking is because you were checking for ___
instead of _
. i.e 3 consecutive spaces instead of one space. That is why the difference always resulted in the same value.
import string
def count_punct(text):
if text.strip() == "": # To take of care of all space input
return 0
count = sum([1 if char in string.punctuation else 0 for char in text ])
spaces = text.count(" ") # Your error is here, Only check for 1 space instead of 3 spaces
total_chars = len(text) - spaces
return round(count / total_chars, 3)*100
text= "DEACTIVATE: OK"
print(count_punct(text))
Outputs:
7.7
And for the zero divide by error. It's a logic error when the total_chars is 0, because the length
of string and number of spaces
both are equal. Hence the difference is 0.
To fix this you can simply add an if statement (already added above)
if text.strip() == "":
print(0)
Note that OP commented quote "The code above is with two spaces (text.count(" ")). The code I am getting error is with single space (text.count(" "))." But you're right about division by zero.
– Tomothy32
Nov 14 '18 at 6:25
@Tomothy32 I'm not getting any error with single space. And the results are always consistent. And why would he want to check for double spaces anyway?
– Vineeth Sai
Nov 14 '18 at 6:32
1
What I meant to say was that OP knew about the fact that they accidentally used multiple spaces, they just didn't edit the post. (Nothing wrong with your answer, sorry for any confusion.)
– Tomothy32
Nov 14 '18 at 6:34
Oh :) No confusion. Thanks for pointing it out.
– Vineeth Sai
Nov 14 '18 at 6:41
Thank you very much, it works now and percentage is correct.
– Vishwas
Nov 14 '18 at 16:47
|
show 1 more comment
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53293781%2fcalculate-punctuation-percentage-in-a-string-in-python%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Here, Make these small changes and your count_punct
function should be up and running.. The reason your code was breaking is because you were checking for ___
instead of _
. i.e 3 consecutive spaces instead of one space. That is why the difference always resulted in the same value.
import string
def count_punct(text):
if text.strip() == "": # To take of care of all space input
return 0
count = sum([1 if char in string.punctuation else 0 for char in text ])
spaces = text.count(" ") # Your error is here, Only check for 1 space instead of 3 spaces
total_chars = len(text) - spaces
return round(count / total_chars, 3)*100
text= "DEACTIVATE: OK"
print(count_punct(text))
Outputs:
7.7
And for the zero divide by error. It's a logic error when the total_chars is 0, because the length
of string and number of spaces
both are equal. Hence the difference is 0.
To fix this you can simply add an if statement (already added above)
if text.strip() == "":
print(0)
Note that OP commented quote "The code above is with two spaces (text.count(" ")). The code I am getting error is with single space (text.count(" "))." But you're right about division by zero.
– Tomothy32
Nov 14 '18 at 6:25
@Tomothy32 I'm not getting any error with single space. And the results are always consistent. And why would he want to check for double spaces anyway?
– Vineeth Sai
Nov 14 '18 at 6:32
1
What I meant to say was that OP knew about the fact that they accidentally used multiple spaces, they just didn't edit the post. (Nothing wrong with your answer, sorry for any confusion.)
– Tomothy32
Nov 14 '18 at 6:34
Oh :) No confusion. Thanks for pointing it out.
– Vineeth Sai
Nov 14 '18 at 6:41
Thank you very much, it works now and percentage is correct.
– Vishwas
Nov 14 '18 at 16:47
|
show 1 more comment
Here, Make these small changes and your count_punct
function should be up and running.. The reason your code was breaking is because you were checking for ___
instead of _
. i.e 3 consecutive spaces instead of one space. That is why the difference always resulted in the same value.
import string
def count_punct(text):
if text.strip() == "": # To take of care of all space input
return 0
count = sum([1 if char in string.punctuation else 0 for char in text ])
spaces = text.count(" ") # Your error is here, Only check for 1 space instead of 3 spaces
total_chars = len(text) - spaces
return round(count / total_chars, 3)*100
text= "DEACTIVATE: OK"
print(count_punct(text))
Outputs:
7.7
And for the zero divide by error. It's a logic error when the total_chars is 0, because the length
of string and number of spaces
both are equal. Hence the difference is 0.
To fix this you can simply add an if statement (already added above)
if text.strip() == "":
print(0)
Note that OP commented quote "The code above is with two spaces (text.count(" ")). The code I am getting error is with single space (text.count(" "))." But you're right about division by zero.
– Tomothy32
Nov 14 '18 at 6:25
@Tomothy32 I'm not getting any error with single space. And the results are always consistent. And why would he want to check for double spaces anyway?
– Vineeth Sai
Nov 14 '18 at 6:32
1
What I meant to say was that OP knew about the fact that they accidentally used multiple spaces, they just didn't edit the post. (Nothing wrong with your answer, sorry for any confusion.)
– Tomothy32
Nov 14 '18 at 6:34
Oh :) No confusion. Thanks for pointing it out.
– Vineeth Sai
Nov 14 '18 at 6:41
Thank you very much, it works now and percentage is correct.
– Vishwas
Nov 14 '18 at 16:47
|
show 1 more comment
Here, Make these small changes and your count_punct
function should be up and running.. The reason your code was breaking is because you were checking for ___
instead of _
. i.e 3 consecutive spaces instead of one space. That is why the difference always resulted in the same value.
import string
def count_punct(text):
if text.strip() == "": # To take of care of all space input
return 0
count = sum([1 if char in string.punctuation else 0 for char in text ])
spaces = text.count(" ") # Your error is here, Only check for 1 space instead of 3 spaces
total_chars = len(text) - spaces
return round(count / total_chars, 3)*100
text= "DEACTIVATE: OK"
print(count_punct(text))
Outputs:
7.7
And for the zero divide by error. It's a logic error when the total_chars is 0, because the length
of string and number of spaces
both are equal. Hence the difference is 0.
To fix this you can simply add an if statement (already added above)
if text.strip() == "":
print(0)
Here, Make these small changes and your count_punct
function should be up and running.. The reason your code was breaking is because you were checking for ___
instead of _
. i.e 3 consecutive spaces instead of one space. That is why the difference always resulted in the same value.
import string
def count_punct(text):
if text.strip() == "": # To take of care of all space input
return 0
count = sum([1 if char in string.punctuation else 0 for char in text ])
spaces = text.count(" ") # Your error is here, Only check for 1 space instead of 3 spaces
total_chars = len(text) - spaces
return round(count / total_chars, 3)*100
text= "DEACTIVATE: OK"
print(count_punct(text))
Outputs:
7.7
And for the zero divide by error. It's a logic error when the total_chars is 0, because the length
of string and number of spaces
both are equal. Hence the difference is 0.
To fix this you can simply add an if statement (already added above)
if text.strip() == "":
print(0)
edited Nov 14 '18 at 6:16
answered Nov 14 '18 at 6:10
Vineeth SaiVineeth Sai
2,48851323
2,48851323
Note that OP commented quote "The code above is with two spaces (text.count(" ")). The code I am getting error is with single space (text.count(" "))." But you're right about division by zero.
– Tomothy32
Nov 14 '18 at 6:25
@Tomothy32 I'm not getting any error with single space. And the results are always consistent. And why would he want to check for double spaces anyway?
– Vineeth Sai
Nov 14 '18 at 6:32
1
What I meant to say was that OP knew about the fact that they accidentally used multiple spaces, they just didn't edit the post. (Nothing wrong with your answer, sorry for any confusion.)
– Tomothy32
Nov 14 '18 at 6:34
Oh :) No confusion. Thanks for pointing it out.
– Vineeth Sai
Nov 14 '18 at 6:41
Thank you very much, it works now and percentage is correct.
– Vishwas
Nov 14 '18 at 16:47
|
show 1 more comment
Note that OP commented quote "The code above is with two spaces (text.count(" ")). The code I am getting error is with single space (text.count(" "))." But you're right about division by zero.
– Tomothy32
Nov 14 '18 at 6:25
@Tomothy32 I'm not getting any error with single space. And the results are always consistent. And why would he want to check for double spaces anyway?
– Vineeth Sai
Nov 14 '18 at 6:32
1
What I meant to say was that OP knew about the fact that they accidentally used multiple spaces, they just didn't edit the post. (Nothing wrong with your answer, sorry for any confusion.)
– Tomothy32
Nov 14 '18 at 6:34
Oh :) No confusion. Thanks for pointing it out.
– Vineeth Sai
Nov 14 '18 at 6:41
Thank you very much, it works now and percentage is correct.
– Vishwas
Nov 14 '18 at 16:47
Note that OP commented quote "The code above is with two spaces (text.count(" ")). The code I am getting error is with single space (text.count(" "))." But you're right about division by zero.
– Tomothy32
Nov 14 '18 at 6:25
Note that OP commented quote "The code above is with two spaces (text.count(" ")). The code I am getting error is with single space (text.count(" "))." But you're right about division by zero.
– Tomothy32
Nov 14 '18 at 6:25
@Tomothy32 I'm not getting any error with single space. And the results are always consistent. And why would he want to check for double spaces anyway?
– Vineeth Sai
Nov 14 '18 at 6:32
@Tomothy32 I'm not getting any error with single space. And the results are always consistent. And why would he want to check for double spaces anyway?
– Vineeth Sai
Nov 14 '18 at 6:32
1
1
What I meant to say was that OP knew about the fact that they accidentally used multiple spaces, they just didn't edit the post. (Nothing wrong with your answer, sorry for any confusion.)
– Tomothy32
Nov 14 '18 at 6:34
What I meant to say was that OP knew about the fact that they accidentally used multiple spaces, they just didn't edit the post. (Nothing wrong with your answer, sorry for any confusion.)
– Tomothy32
Nov 14 '18 at 6:34
Oh :) No confusion. Thanks for pointing it out.
– Vineeth Sai
Nov 14 '18 at 6:41
Oh :) No confusion. Thanks for pointing it out.
– Vineeth Sai
Nov 14 '18 at 6:41
Thank you very much, it works now and percentage is correct.
– Vishwas
Nov 14 '18 at 16:47
Thank you very much, it works now and percentage is correct.
– Vishwas
Nov 14 '18 at 16:47
|
show 1 more comment
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53293781%2fcalculate-punctuation-percentage-in-a-string-in-python%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
The code above is with two spaces (text.count(" ")). The code I am getting error is with single space (text.count(" ")).
– Vishwas
Nov 14 '18 at 5:39
That's not how percentage works. You don't subtract the number of punctuation characters, it's just
(number of punctuation characters) / (length of string)
, unless you mean subtract the whitespace, not the punctuation.– Tomothy32
Nov 14 '18 at 5:41
you mean like this "return round((count)/(len(text) - text.count(" ")), 3)*100"? I still get same error.
– Vishwas
Nov 14 '18 at 5:43
Your error is division by zero? That means you passed in some string that is either fully spaces or empty.
– Tomothy32
Nov 14 '18 at 5:47
Thats what I am not able to figure out. Any suggestion or advice will be much appreciated.
– Vishwas
Nov 14 '18 at 5:50