2 Processing and formatting strings

 

This chapter covers

  • Using f-strings to interpolate expressions and apply formatting
  • Converting strings to other applicable data types
  • Joining and splitting strings
  • Using regular expressions for advanced string processing

Textual information is the most important form of data in almost every application. Textual data as well as numeric data can be saved as text files, and reading them requires us to process strings. On a shopping website, for example, we use text to provide production descriptions. Machine learning is trending, and you may have heard about one machine learning specialty: natural language processing, which extracts information from texts. Because of the universal use of strings, text processing is an inevitable step in preparing data in these scenarios. Using our task management app as the context, we need to convert a task’s attributes to textual data so that we can present them at the frontend of our web app. When we obtain data entry at the frontend of our app, we must convert these strings to a proper type, such as an integer, for further processing. In numerous real-life cases like these, we need to process and format strings properly. In this chapter, we tackle some common text processing problems.

2.1 How do I use f-strings for string interpolation and formatting?

 

2.1.1 Formatting strings before f-strings

 
 

2.1.2 Using f-strings to interpolate variables

 

2.1.3 Using f-strings to interpolate expressions

 
 

2.1.4 Applying specifiers to format f-strings

 
 
 
 

2.1.5 Discussion

 

2.1.6 Challenge

 
 
 

2.2 How do I convert strings to retrieve the represented data?

 

2.2.1 Checking whether strings represent alphanumeric values

 
 
 

2.2.2 Casting strings to numbers

 
 

2.2.3 Evaluating strings to derive their represented data

 

2.2.4 Discussion

 
 

2.2.5 Challenge

 

2.3 How do I join and split strings?

 
 
 

2.3.1 Joining strings with whitespaces

 
 
 
 

2.3.2 Joining strings with any delimiters

 
 
sitemap

Unable to load book!

The book could not be loaded.

(try again in a couple of minutes)

manning.com homepage
test yourself with a liveTest