4. String Comparison
4
• The ways to compare two strings in Python are
• By using == (equal to) operator
• By using != (not equal to) operator
• By using sorted() method
• By using is operator
• By using Comparison operators
9. String Comparison
9
By using comparison/ relational operators
• Python string comparison is performed using the characters in both
strings
• The characters in both strings are compared one by one
• When different characters are found then their Unicode value is
compared
• The character with lower Unicode value is considered to be smaller
12. String Formatting
12
• String formatting is the process of infusing things in the string dynamically
and presenting the string
• There are four different ways to perform string formatting:
• Formatting with % Operator
• Formatting with format() string method
• Formatting with string literals, called f-strings
• Formatting with String Template Class
13. String Formatting
13
Formatting with % Operator
• Oldest method of string formatting
%5.3f
• Minimum number of
digits to be present in
the string.
• These might be padded
with white space if the
whole number doesn’t
have this many digits.
• Represents how many
digits are to be
displayed after the
decimal point
15. String Formatting
15
Formatting with format() String Method
• Format() method was introduced with Python3 for handling complex
string formatting more efficiently
• Formatters work by putting in one or more replacement fields and
placeholders defined by a pair of curly braces { } into a string and calling
the str.format()
• The value we wish to put into the placeholders and concatenate with the
string passed as parameters into the format function.
• Benefit of this method is that we can insert object by using index-based
position
• Syntax: ‘String { } then { }’.format(‘something1′,’something2’)
17. String Formatting
17
Formatting Float Precision with format() String Method
• Syntax: ‘[index]:[width][.precision][type]}
• The type can be used with format codes:
• ‘d’ for integers
• ‘f’ for floating-point numbers
• ‘b’ for binary numbers
• ‘o’ for octal numbers
• ‘x’ for octal hexadecimal numbers
• ‘s’ for string
• ‘e’ for floating-point in an exponent format
19. String Formatting
19
Formatting Float Precision with format() String Method
• Since, no alignment is specified, it
is aligned to the right
• Takes a width of 8 as a whole
leaving 2 places to the left.
• Fill the remaining places with zero
20. String Formatting
20
Formatting String with F-Strings
• To create an f-string, prefix the string with the letter “ f ”
• The string itself can be formatted in much the same way that you would
with str.format()
• F-strings provide a concise and convenient way to embed python
expressions inside string literals for formatting
23. String Slicing
23
• Python slicing is about obtaining a sub-string from the given string by
slicing it respectively from start to end
• Python slicing can be done in two ways
• slice() Constructor
• Extending Indexing
24. String Slicing
24
• Slice() Constructor
• The slice() constructor creates a slice object representing the set of
indices specified by range(start, stop, step)
• Syntax:
• slice(stop)
• slice(start, stop, step)
• Parameters:
• start: Starting index where the slicing of object starts
• stop: Ending index where the slicing of object stops
• step: It is an optional argument that determines the increment
between each index for slicing
• Return Type: Returns a sliced object containing elements in the
given range only.
25. String Slicing
25
• Slice() Constructor
Index tracker for positive and negative index:
• Negative comes into considers when tracking the string in reverse.
26. String Slicing
26
• Slice() Constructor
• Negative comes into considers when tracking the string in reverse.
27. String Slicing
27
• Extending Indexing
• In Python, indexing syntax can be used as a substitute for the slice
object. This is an easy and convenient way to slice a string both
syntax wise and execution wise.
• Syntax: string[start:end:step]
32. String Splitting
32
• The split() method splits a string into a list
• Syntax: string.split(separator, maxsplit)
• separator: Optional. Specifies the separator to use when
splitting the string. By default, any whitespace is a separator
• max split: Optional. Specifies how many splits to do. Defaulty
value is -1, which is “all occurrences”
37. String Stripping
37
• The strip() method removes any leading (spaces at the beginning)
and trailing (spaces at the end) characters (space is the default
leading character to remove)
• Syntax: string.strip(characters)
• characters: Optional. A set of characters to remove as leading/
trailing characters.
41. Regular Expression
41
• Regular Expressions, often shortened as regex, are a sequence of
characters used to check whether a pattern exists in a given text (string)
or not
• The Python module re provides full support for Perl-like regular
expressions in Python.
• Its primary function is to offer a search, where it takes a regular
expression and a string. Here, it either returns the first match or else
none.