Operations On “String”
--
“Strings” in “Python” are what are called “Sequence Types”, which means that “Strings” support certain “Common Operations” for “Querying Sequences”.
“Indexing” of “Strings”
- It is possible to “Access” the “Individual Characters” of a “String” using the “Square Brackets”, i.e., “[]”, with an “Integer 0-Based Index”.
- “In Contrast” to many “Programming Languages”, there is “No Separate Data Type”, called the “Character” Data Type in “Python”, which is “Distinct” from the “String” Data Type.
- The “Indexing” Operation “Returns” a “Full-Blown String” that can “Contain” a “Single Character Element” or “Multiple Character Elements”.
“Determine” the “Length” of “Strings”
- It is possible to “Determine” the “Length” of a “String” using the “Built-In Python Function”, called “len ()”.
“Concatenation” of the “Strings” Using the “Plus Operator”
- It is possible to “Concatenate” the “String” using the “Plus Operator”, i.e., “+”.
“Concatenation” of the “Strings” Using the “Augmented Assignment Operator”
- It is also possible to use the “Augmented Assignment Operator”, i.e., “+=”, to “Concatenate” the “Multiple Strings” in “Python”.
- Since “Strings” are “Immutable”, the “Augmented Assignment Operator”, i.e., “+=”, would “Bind” a “New String Object” to the “String Object Reference” on “Each Use”.
- In “Concatenating” the “Multiple Strings” using the “Augmented Assignment Operator”, the “Illusion” of “Modifying” the “String Object Reference”, i.e., “myName” “In-Place” is “Achievable”, because, the “myName” is a “Reference” to a “String Object”, and “Not” the “String Object” itself.
“Methods” of “Strings”
- “String Objects” also support a wide variety of “Operations” that are “Implemented” as “Methods”.
- To “Call” the “Methods” on the “Objects” in “Python”, the “Dot”, i.e., “.” is used “After” the “Object Name”, and, “Before” the “Method Name”.
- “Methods” are “Functions”. So, the “Parentheses” must be used to “Indicate” that the “Method” should be “Called”.
- Since “Strings” are “Immutable”, any “Method” that is “Applied” on a “String Object”, “Does Not Modify” the “String Object In-Place”. Rather “Returns” a “New String Object”.
Example — “Capitalize” the “First Letter” of a “String”
“Concatenation” of the “Strings” Using the “join ()” Method
- The “join ()” Method in the “String” Class “Takes” the “Multiple String Objects” to be “Concatenated” as an “Argument” as a “Collection” Data Type, like — “List”, “Tuple” etc., and, “Produces” a “New String Object”, by “Inserting” a “Separator” between “Each” of the “Input String Objects”.
- An interesting aspect of the “join ()” Method is “How” the “Separator” is specified. The “join ()” Method is “Called” on the “Separator String”.
Why the “join ()” Method is the “Preferred Approach” for “String Concatenation”?
- The “join ()” Method should be “Preferred” for “Concatenating”, i.e., “Joining” the “Large Numbers” of “Strings”, because, the “join ()” Method is substantially “More Efficient”, because, the “Concatenation” of “Strings” with the “Plus/Addition Operator”, or, with the “Augmented Assignment Operator” can “Lead” to the “Generation” of “Large Numbers” of “Temporary String Objects” with “Consequent Costs” for “Memory”, “Allocation” and “Copies”.
“Splitting” of the “Strings” Using the “split ()” Method
- The “split ()” Method in the “String” Class is “Called” on a “String Object”, and, “Produces” “Multiple New String Objects”, by “Splitting” the “Input String Object” based on an “White Space String” as a “Separator”, by default.
“Splitting” of the “Strings” Using the “split ()” Method by Passing “Optional Argument”
- It is also possible to “Split” a “String” using any other “Separator” that is “Not” the “Default Separator”, i.e., the “White Space String” by “Passing” the “Separator Value” in the “split ()” Method.
“Partitioning” a “String” Using the “partition ()” Method
- The “partition ()” Method in the “String” Class is “Called” on a “String Object”, and, “Takes” a “Separator String” as an “Argument”, and, then “Divides” the “Input String” into “Three Sections” -
1. The part “Before the Separator”
2. The “Separator” itself
3. The part “After the Separator” - “String Partitioning” using the “partition ()” Method “Returns” a “Tuple”. Hence, this Method is “Commonly Used” in “Conjunction With” the “Tuple Unpacking”.
- Often the “Developers” are “Not Interested” in “Capturing” the “Separator Value”. So, to “Capture” the “Separator Value”, most of the times, the “Underscore Variable”, i.e., “_” is used.
- This is “Not Treated in a Special Way” by “Python”, but, there is an “Unwritten Convention” that the “Underscore Variable” is used for “Unused”, or, “Dummy” Values. This “Convention” is supported by many “Python-Aware Development Tools”, which will “Supress” the “Unused Variable Warnings” for “Underscore Variable”.
“Formatting” of a “String” Using “format ()” Method
One of the most interesting and frequently used “String Method” is “format ()”.
- The “format ()” Method “Supersedes”, although “Does Not Replace” the “String Interpolation Technique” that was used in the “Older Versions” of “Python”.
- The “format ()” Method can be “Called” on any “String” that “Contains” the so-called “Replacement Fields”, which are “Surrounded By” the “Curly Braces”.
- The “Objects” that are provided as the “Arguments” to the “format ()” Method are “Converted” to “Strings” and used to “Populate” the “Replacement Fields”.
1. Using “format ()” Method With the “Positional Arguments”
- In case of the “format ()” Method with the “Positional Arguments”, the “Replacement Field Numbers”, which are “Positive Integer Numbers”, can be used to “Match Up” with the “Positional Arguments” that are sent to the “format ()” Method.
- In the following example, the “Positional Arguments” that are sent to the “format ()” Method are -
1. A “String” with the “Value” as “Oindrila”
2. An “Integer” with the “Value” as “34”
These “Positional Arguments” will be “Inserted” into the “String” to be “Formatted”. - For the following example, the “Replacement Field Numbers” are “Specified” by “0”, and, “1”, which are “Matched Up” with the “Positional Arguments”, i.e., “Oindrila”, and, “34” respectively, which are sent to the to “format ()” Method, and, “Each” of the “Positional Arguments” are “Converted” to “Strings”.
In case of the “format ()” Method with the “Positional Arguments”, the “Replacement Field Numbers” may be used “More Than Once”.
If the “Replacement Fields” are used “Exactly Once”, and, in the “Same Order” as the “Positional Arguments” that are sent to the to “format ()” Method, the “Numbers” from the “Replacement Fields” can be “Omitted”.
It is possible to “Specify” the “Replacement Field Numbers” in the “String” to be “Formatted” in a “Different Order” than the “Order” in which the “Positional Arguments” are sent to the “format ()” Method.
2. Using “format ()” Method With the “Keyword Arguments”
- In case of the “format ()” Method with the “Keyword Arguments”, the “Named Replacement Fields” can be used, “Instead Of” the “Ordinals”.
- In the following example, the “Keyword Arguments” that are sent to the “format ()” Method are -
1. name: A “String” with the “Value” as “Oindrila”
2. age: An “Integer” with the “Value” as “34”
These “Keyword Arguments” will be “Inserted” into the “String” to be “Formatted” to the corresponding “Named Replacement Fields”. - For the following example, the “Named Replacement Fields” are “Specified” by “name”, and, “age”, which are “Matched Up” with the “Keyword Arguments”, i.e., “name”, and, “age” respectively, which are sent to the to “format ()” Method, and, “Each” of the “Keyword Arguments” are “Converted” to “Strings”.
In case of the “format ()” Method with the “Keyword Arguments”, the “Named Replacement Fields” may be used “More Than Once”.
It is possible to “Specify” the “Named Replacement Fields” in the “String” to be “Formatted” in a “Different Order” than the “Order” in which the “Keyword Arguments” are sent to the “format ()” Method.
It is possible to provide “Index” into the “Sequences” using a “Square Brackets”, i.e., “[]”, inside the “Named Replacement Fields” when a “Collection Data Type” is sent as a “Keyword Argument” to the “format ()” Method.
To “Use” a “Value” that is “Present” at a particular “Index” in the “Keyword Argument”, i.e., a “Collection Data Type”, in the “String” to be “Formatted”, the corresponding “Index” can be provided with the “Named Replacement Field”.
It is possible to “Access” the “Object Attributes” from the “String” to be “Formatted”, i.e., from within the “Named Replacement Fields” in the “String” to be “Formatted”, where an “Entire Object” can be “Passed” as the “Keyword Argument” to the “format ()” Method.
A “Python Module” can be “Passed” as a “Keyword Argument” to the “format ()” Method, because, “Python Modules” are “Objects”.
The “format ()” Method gives a lot of “Control” over the “Field Alignment” and “Floating-Point Formatting”.
“Issues” of “Formatting” a “String” Using “format ()” Method
- While the “format ()” Method is “Quite Powerful” and generally “Preferrable” to its “Predecessors”, it could be “Quite Verbose”, even for the “Relatively Simple Cases”.
- In case of “Complex String Interpolation”, the “Variables”, containing “Values”, need to be kept “In-Place” for “Readability” and “Maintainability”. That means, it is the “Developer’s Responsibility” to “Keep Track” and “Maintain” which “Variable” will be used to “Pass” the “Value” to which “Keyword Argument” in the “format ()” Method.
- Example — if an “Integer Expression” is “Assigned” to a “Variable”, and then, if the “Calculated Value” of that “Variable” is “Interpolated” into a “String” using the “format ()” Method’s “Keyword Argument” Feature, it is the “Developer’s Responsibility” to provide the “Name” of the “Variable” to the “Proper” “Keyword Argument” in the “format ()” Method.
“Formatting” of a “String” Using “Literal String Interpolation”, or, Commonly Known as “f-strings”
- “F-Strings” are “Available” in “Python 3.6”, and, later.
- “F-Strings” provide a way to “Embed” the “Expressions” inside the “Literal Strings” using a “Minimal Syntax”.
- An “F-String” is like a “Normal Literal String”, except that, it is “Prefixed” with the “Letter f”.
- Inside the “Literal Strings”, “Python Expressions” can be “Embedded” inside the “Curly Braces”, i.e., “{}”, and, the “Results” of these “Expressions” will be “Inserted” into the “Literal String” at “Run Time”.
Because the “F-Strings” allow to use any “Python Expression”, the “Developers” are “Not Limited” to use only “Simple Named References”, but also can “Call Functions” using the “F-Strings”.
It is possible to “Access” the “Object Attributes” from within a “Literal String” by “Accessing” the “Object Attributes” from within the “Curly Braces” of the “F-String”.
The “F-String” approach supports the “Floating-Point Formatting”.