Count number of characters: Difference between revisions
Jump to navigation
Jump to search
python
m (Text replacement - "Category:Text file processing" to "Category:String manipulation") |
(python) |
||
| Line 106: | Line 106: | ||
Number of characters (not contains the [[Return symbol | return symbol]]) = result of {{kbd | key=<nowiki>wc -m *.txt</nowiki>}} - result of {{kbd | key=<nowiki>wc -m *.txt</nowiki>}} * 2 - 1 (the last blank line costs 1 character) - number of the whitespaces | Number of characters (not contains the [[Return symbol | return symbol]]) = result of {{kbd | key=<nowiki>wc -m *.txt</nowiki>}} - result of {{kbd | key=<nowiki>wc -m *.txt</nowiki>}} * 2 - 1 (the last blank line costs 1 character) - number of the whitespaces | ||
== Python == | |||
Using the [https://docs.python.org/3/library/functions.html#len len()] function<ref>[https://stackoverflow.com/questions/30686701/python-get-size-of-string-in-bytes Python : Get size of string in bytes - Stack Overflow]</ref>. Try it on [https://replit.com/@planetoid/lenth-of-string#main.py replit]. | |||
=== Get the number of characters in a string in Python === | |||
<pre> | |||
string = "狐" | |||
print(len(string)) | |||
// returns 1 | |||
</pre> | |||
=== Get the number of bytes in a string in Python === | |||
<pre> | |||
string = "狐" | |||
print(len(string.encode('utf-8'))) | |||
// returns 3 | |||
print(len(string.encode('utf-16-le'))) | |||
// returns 2 | |||
</pre> | |||
== References == | |||
<references /> | |||
[[Category:Software]] [[Category:Programming]] [[Category:Data Science]] [[Category:String manipulation]] [[Category:Data transformation]] | [[Category:Software]] [[Category:Programming]] [[Category:Data Science]] [[Category:String manipulation]] [[Category:Data transformation]] | ||
[[Category:Regular expression]] [[Category:PHP]] [[Category:MySQL]] | [[Category:Regular expression]] [[Category:PHP]] [[Category:MySQL]] | ||