python_wiki:string_normalization

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

python_wiki:string_normalization [2019/05/25 23:50]
python_wiki:string_normalization [2019/05/25 23:50] (current)
Line 1: Line 1:
 +====== String Normalization ======
 +
 +**General Information**
 +
 +Normalizing strings prior to manipulations and comparisons. 
 +
 +**Checklist**
 +  * Python 2 or 3
 +
 +----
 +
 +====== Usage ======
 +
 +Using the code snippet.
 +<code bash>
 +chmod +x string-normalizing.py
 +./string-normalizing.py
 +</code>
 +
 +----
 +
 +====== The Code ======
 +
 +<code python string-normalizing.py>
 +#!/usr/bin/python
 +
 +# strings for testing
 +my_string1=" Red Hat Linux "
 +my_string2=" CentOS"
 +my_string3="Debian "
 +my_string4="Ubuntu Linux"
 +
 +my_string_list = [my_string1, my_string2, my_string3, my_string4]
 +
 +##-- Various Methods to Normalize Your Strings Prior to Comparison --##
 +for string in my_string_list:
 +  # Original String
 +  print("Original string: " + string)
 +
 +  # Convert to all lowercase
 +  print("String all lowercase: " + string.lower())
 +
 +  # Convert to all uppercase
 +  print("String all uppercase: " + string.upper())
 +
 +  # Strip all white spaces from the left
 +  print("Left strip white spaces: " + string.lstrip())
 +
 +  # Strip all white paces from the right
 +  print("Right strip white spaces: " + string.rstrip())
 +
 +  # Strip both left and right spaces
 +  print("Strip both left and right white spaces: " + string.strip())
 +</code>
 +
 +----
  
  • python_wiki/string_normalization.txt
  • Last modified: 2019/05/25 23:50
  • (external edit)