Category: String lists and tuples

String lists and tuples

Comparing strings in python

Comparing strings in python

Comparing strings is a common job in programming. We can compare two strings with a == operator. We can check the opposite with the non-equality != operator. The operators return a boolean True or...

Tuples

Tuples

Tuples are another container type extremely similar in nature to lists. The only visible difference between tuples and lists is that tuples use parentheses and lists use square brackets. Functionally. there is a more...

List Type Built-in Methods

List Type Built-in Methods

You may recall our earlier discussion of accessing object attributes using the dotted attribute notation: object.attribute. List methods are no different, using list.method([arguments]). We use the dotted notation to access the attribute (here it...

list() in python

list() in python

The list() and tuple() methods take sequence types and convert them to lists and tuples, respectively. Although strings are also sequence types, they are not commonly used with list() and tuple(). These built-in functions...

max() and min()

max() and min()

max() and min() did not have a significant amount of usage for strings since all they did was to find the “largest” and “smallest” characters (lexicographically) in the string. For lists (and tuples), their...

finding length (len())

finding length (len())

For strings, len() gives the total length of the string, as in the number of characters. For lists (and tuples), it will not surprise you that len() returns the number of elements in the...

Membership (in, not in)

Membership (in, not in)

With strings, the membership operator determined whether a single character is a member of a string. With lists (and tuples), we can check whether an object is a member of a list (or tuple)....

Format Operator (%)

Format Operator (%)

One of Python’s coolest features is the string format operator. This operator is unique to strings and makes up for the pack of having functions from C’s printf() family. In fact, it even uses...

Strings Concatenation with (+)

Strings Concatenation with (+)

Example The last example illustrates using the concatenation operator to put together a pair of slices from string s, the “Spa” from “Spanish” and the “M” from “Made.” The extracted slices are concatenated and...

Strings in Python

Strings in Python

Strings are amongst the most popular types in Python. We can create them simply by enclosing characters in quotes. Python treats single quotes the same as double quotes. This contrasts with most other scripting...