TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to PYTHON

Python Punctuation and Whitespace (string.punctuation)

Use the string.punctuation property and print all punctuation. Test for punctuation chars like periods and commas.
Punctuation. A string contains letters, whitespace, numbers. And it has punctuation: these characters include commas and periods and semicolons.Strings
With Python, we can access the string.punctuation constant. This contains all the common punctuation characters. It can be tested and used in programs.
Loop example. In this first example we use a for-in loop over the string.punctuation characters. We print each character with surrounding brackets.for

Note: The string.punctuation values do not include Unicode symbols or whitespace characters.

Python program that uses string.punctuation import string # Display punctuation. for c in string.punctuation: print("[" + c + "]") Output [!] ["] [#] [$] [%] [&] ['] [(] [)] [*] [+] [,] [-] [.] [/] [:] [;] [<] [=] [>] [?] [@] [[] [\] []] [^] [_] [`] [{] [|] [}] [~]
In operator use. Here we use the in-operator on the string.punctuation constant. This allows us to test whether a char in a string is a punctuation character.In
Python program that tests for punctuation import string # An input string. name = "hey, my friend!" for c in name: # See if the char is punctuation. if c in string.punctuation: print("Punctuation: " + c) Output Punctuation: , Punctuation: !
Remove punctuation. With the "in" operator and the string.punctuation constant, we can remove all punctuation chars from a string.

Note: We add each character to our result that is not punctuation. Spaces (which are not punctuation) are kept.

Python program that removes punctuation from string import string def remove_punctuation(value): result = "" for c in value: # If char is not punctuation, add it to the result. if c not in string.punctuation: result += c return result # Test our method. temp = "hello, friend!... welcome." print(temp) print(remove_punctuation(temp)) Output hello, friend!... welcome. hello friend welcome
Whitespace. Let us look at another occasionally-helpful constant in the string module: string.whitespace. This too can be looped over or tested.

Tip: Instead of testing constant strings with "in," consider using methods like isspace().

Python program that uses string.whitespace import string print(" " in string.whitespace) print("\n" in string.whitespace) print("X" in string.whitespace) Output True True False
For performance, the in-operator is fast. But it will not outperform a specialized lookup table. We could use a dictionary to store a value indicating whether a char is punctuation or not.Dictionary
A review. Python contains many helpful constants in its string module. For example, we can test whitespace, digits, punctuation. We avoid writing these characters out in special code.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf