Demystifying Text and Numbers: Strings, Numeric Data Types, and Regular Expressions in Python
Tags: python, datatype, devops
Published: 2024-03-24 19:52:54

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
```

```
#string length

course = "python for devops training"
lengthOfTheCourse = len(course)
print("the length of the course is:", lengthOfTheCourse)
```

```
# 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)

```

```
# string-replace

trainningDetails = "Python for DevOps Course"

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

```

```
# string-split

trainningDetails = "Python for DevOps Course"
splitingTrainingDetails = trainningDetails.split()
print ("The splited format look this way:", splitingTrainingDetails )
```

```
# 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 ) 

```

```
#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")

```
2. 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
```

- Floating-point Numbers (float): Numbers with decimal points, representing real numbers.

```
pi = 3.14159
gravity = -9.81
```

- Complex Numbers (complex): Numbers consisting of a real part and an imaginary part (represented by the letter j).

```
voltage = 3+2j
imaginary_unit = 1j
```
These numeric data types allow you to perform various mathematical operations like addition, subtraction, multiplication, and division.

3. 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")
```

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")
```

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

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
```