🧵 Section 1.12: Working with Strings
Using str()
and repr()
Functions for String Operations
In Python, strings can be manipulated and managed using various built-in functions, including str()
and repr()
. These two functions serve similar yet distinct purposes when dealing with strings.
str()
Function
- Purpose: Converts an object to a string suitable for human-readable format, often used for display purposes.
today
: A dictionary representing a date.str(today)
: Converts the dictionary into a string format that is easy to read.
Example:
# Example of str() function
today = {"year": 2023, "month": "September", "day": 5}
print(str(today))
repr()
Function
- Purpose: Converts an object to a string that is more of a formal representation, often one that can be used to recreate the object. Useful for debugging.
repr(today)
: Provides a string that represents how the dictionary might be printed in the Python shell. This representation is more detailed and aimed at developers.
Example:
# Example of repr() function
today = {"year": 2023, "month": "September", "day": 5}
print(repr(today))
Advanced String Formatting
Python provides powerful tools for formatting strings which are essential for creating outputs that are both functional and aesthetically pleasing.
String Formatting with str.format()
- Allows more extensive control over how values are formatted and presented in strings.
{0}
and{1}
are placeholders for the variablesname
andage
, respectively..format(name, age)
: Injects the values ofname
andage
into the placeholders.
Example:
# Using str.format() for advanced formatting
name = "Alice"
age = 30
print("My name is {0} and I am {1} years old.".format(name, age))
Formatted String Literals (f-strings)
- Introduced in Python 3.6, f-strings offer a concise and readable way to include the value of Python expressions inside string literals.
f"{name} is {age + 5} years old in five years."
: Thef
before the quotation marks indicates that it is an f-string. Expressions like{age + 5}
are evaluated at runtime and formatted using their natural string representation.
Example:
# Using f-strings for inline expression evaluation
name = "Bob"
age = 25
print(f"{name} is {age + 5} years old in five years.")
Padding and Alignment in Formatting
- String formatting methods also allow for padding and aligning text.
<
,>
, and^
are format specifiers used to align the text within a specified width.
Example:
# Text alignment with padding
text = "left aligned"
print(f"{text:<20}") # Left align text in a field of width 20
print(f"{text:>20}") # Right align text in a field of width 20
print(f"{text:^20}") # Center text in a field of width 20
Conclusion
Working with strings in Python is made incredibly efficient and flexible due to its powerful built-in functions and formatting capabilities. Whether you need to produce human-readable output or debug and inspect program data, Python’s string functions, including str()
, repr()
, and various formatting options, provide the necessary tools to do so effectively. These capabilities are fundamental in making the code not only functional but also intuitive and maintainable.