DEV Community

Cover image for Demystifying Text and Numbers: Strings, Numeric Data Types, and Regular Expressions in Python
Eunice js
Eunice js

Posted on

Demystifying Text and Numbers: Strings, Numeric Data Types, and Regular Expressions in Python

In the realm of Python programming, data types are the fundamental building blocks used to represent and manipulate information. This article delves into three essential data types: strings, numeric data types, and regular expressions (regex), exploring their functionalities and how they work together for powerful text and number manipulation.

  1. Strings: The Versatile Text Containers

Strings, represented by the str data type in Python, are sequences of characters enclosed in single or double quotes. They are the workhorses for handling textual data, allowing you to store and manipulate words, sentences, paragraphs, or any combination of characters.

Here are some key aspects of strings:

programmingLaguage = "python"
print(programmingLanguage)

  • Accessing Characters: Individual characters within a string can be accessed using their index (starting from 0).

first_letter = name[0] # first_letter will be "A"

  • String Operations: Python offers a rich set of built-in methods for string manipulation, including concatenation (joining strings), slicing (extracting substrings), searching, and more.
full_name = name + " Smith"  # Concatenation
last_name = full_name[6:]  # Slicing
Enter fullscreen mode Exit fullscreen mode
#string length

course = "python for devops training"
lengthOfTheCourse = len(course)
print("the length of the course is:", lengthOfTheCourse)
Enter fullscreen mode Exit fullscreen mode
# uppercase and lower case

trainningDetails = "Python for DevOps"

trainingInUpperCase = trainningDetails.upper()

trainingInLowerCase = trainningDetails.lower()

print ("the training written in uppercase is displayed this way:", trainingInUpperCase)
print ("The training written in lowercase is dispalayed this way:", trainingInLowerCase)

Enter fullscreen mode Exit fullscreen mode
# string-replace

trainningDetails = "Python for DevOps Course"

trainningDetailsNewTitile = trainningDetails.replace( "Course", "training")
print ("Now we have a new title which is:", trainningDetailsNewTitile)

Enter fullscreen mode Exit fullscreen mode
# string-split

trainningDetails = "Python for DevOps Course"
splitingTrainingDetails = trainningDetails.split()
print ("The splited format look this way:", splitingTrainingDetails )
Enter fullscreen mode Exit fullscreen mode
# string-strip
trainningDetails = "                                      Python for DevOps Course"
print ("The strip format look this way:", trainningDetails)
stripTrainingDetails = trainningDetails.strip()
print ("The strip format look this way:", stripTrainingDetails ) 

Enter fullscreen mode Exit fullscreen mode
#string-substring

trainningDetails = "Python for DevOps Course"
substring = "is"
if substring in trainningDetails:
    print (substring, "found in the text trainingDetails")
else:
    print(substring, "not found in text trainingDetails")

checkWordsAvailiability = "getting interesting so far"
substring = "interesting"
if substring in checkWordsAvailiability:
    print(substring, "is actually part of the word")
else:
    print(substring, "is not part of the word")

Enter fullscreen mode Exit fullscreen mode
  1. Numeric Data Types: Representing Numbers

Python provides several data types to represent different kinds of numbers:

Integers (int): Whole numbers, positive, negative, or zero. They can be arbitrarily large.

age = 30
count = -10
large_number = 9999999999999999999
Enter fullscreen mode Exit fullscreen mode
  • Floating-point Numbers (float): Numbers with decimal points, representing real numbers.
pi = 3.14159
gravity = -9.81
Enter fullscreen mode Exit fullscreen mode
  • Complex Numbers (complex): Numbers consisting of a real part and an imaginary part (represented by the letter j).
voltage = 3+2j
imaginary_unit = 1j
Enter fullscreen mode Exit fullscreen mode

These numeric data types allow you to perform various mathematical operations like addition, subtraction, multiplication, and division.

  1. Regular Expressions (regex): Powerful Text Patterns

Regular expressions, often shortened to regex, are a concise and expressive way to define patterns within text data. They act like powerful search filters, enabling you to find, extract, or validate specific sequences of characters based on predefined rules.

In Python, the re module provides functionalities for working with regex. Here are some common uses of regex:

Matching Text Patterns: Check if a string adheres to a particular format, like an email address or a phone number.

import re

handsOnLabOnMatch = "learning how the regression match works"
CheckingForThePattern = r".*works*"
usingTheMatchWord = re.match(CheckingForThePattern, handsOnLabOnMatch)
if usingTheMatchWord:
    print("match found:", usingTheMatchWord.group())
else:
    print("match not found")
Enter fullscreen mode Exit fullscreen mode

Extracting Information: Grab specific parts of a string that match a pattern, such as extracting digits from a product code.

import re

imaliStaffs = "sean" "nelly" "scentry" "eunice"
checkingForStaffs = r"eunice"
searchForTheName = re.search(checkingForStaffs, imaliStaffs)
if searchForTheName:
    print( searchForTheName.group(), "the person in particlar is part of the staff")
else:
    print("not a staff")
Enter fullscreen mode Exit fullscreen mode

Replacing Text: Substitute specific patterns within a string with new text.

import re
theCompleteWord = "python is a programming culture"
thePattern = r"culture"
theWordToReplacwWith = "language"
replacingTheWord = re.sub(thePattern, theWordToReplacwWith, theCompleteWord) 
print("modified text:", replacingTheWord)
Enter fullscreen mode Exit fullscreen mode

The Synergy Between Strings, Numbers, and Regex

While these data types seem distinct, they often work together in powerful ways:

  • Validating Numeric Input:
    Use regex to ensure user input for a product quantity consists only of digits.

  • Extracting Numbers from Text:
    Employ regex to extract numeric values like prices or dates embedded within text data.

  • Formatting Numbers as Strings:
    Convert numeric values to strings with specific formatting, like currency symbols or number of decimal places.

Here's an example showcasing how regex can be used with numeric strings:

import re

# Sample text data with prices
text = "The price of a banana is $1.25, while an apple costs ₦1,500.00."

# Regex pattern for numeric string with currency symbol
price_pattern = r"[\$|\₦](\d+\.\d{2})"  # Matches currency symbol, digits, decimal, two digits

# Find all price occurrences using regex
prices = re.findall(price_pattern, text)

# Extract numeric values and convert to float
for price_str in prices:
  price_value = float(price_str.replace(",", ""))  # Remove comma if present, convert to float
  print(f"Extracted price: {price_value:.2f}")  # Format price with 2 decimal places
Enter fullscreen mode Exit fullscreen mode

Top comments (1)

Collapse
 
scentre profile image
scentre

Great article