[[python_wiki:string_normalization]]

String Normalization

General Information

Normalizing strings prior to manipulations and comparisons.

Checklist

  • Python 2 or 3

Usage

Using the code snippet.

chmod +x string-normalizing.py
./string-normalizing.py

The Code

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())

  • python_wiki/string_normalization.txt
  • Last modified: 2019/05/26 03:50
  • (external edit)