3 minute read

Python Strings Method startswith()

Next Article
Syntax

Syntax

If the string begins with the given value, the startswith() method provides True, else False.

Syntax string.startswith(value, start, end)

Advertisement

Parameter Values

Parameter Description value It is necessary. To find out if the string begins with this value start A choice. An integer specifying where the search should begin end A choice. The location at which the search should terminate is an integer.

Make sure the string begins with “Greetings”: mrx = "Greetings, we are learning about the Python startswith() method" ample = mrx startswith("Greetings") print(ample)

Verify that the string begins with “Greetings”: mrx = "Greetings, we are learning about the Python startswith() method" mrx = mrx.lower() ample = mrx.startswith("Greetings") print(ample)

Make sure position 18 to 40 begins with “lear”: mrx = "Greetings, we are learning about the Python startswith() method" ample = mrx.startswith("lear",18, 40) print(ample)

Python Strings Method strip()

The strip() method eliminates preceding (spaces at the beginning) and following (spaces at the last) characters (by default, a space is the leading character).

Syntax string.strip(characters)

Parameter Values

Parameter Description

characters A choice. Characters to eliminate as preceding/following characters.

Take out the spaces at the start and at the tail of the string:

5 6 7 8 9 10 mrx = " BEST " ample = mrx.strip() print("Python is the", ample,"Programming Language")

Pass the value “*” in the rstrip() method: mrx = "**********BEST**********" ample = mrx strip("*") print("Python is the", ample,"Programming Language")

Take out the preceding and following characters:

10 mrx = ">" ample = mrx.strip("") print("Python is the", ample,"Programming Language")

Python Strings Method swapcase()

By calling the swapcase() method, you can get a string that includes all uppercase letters transformed into lowercase letters, and the reverse.

Syntax string swapcase()

Parameters not required – Change the small case letters to capital case and the capital case letters to small case: mrx = "hApPy bIRth dAy harry" ample = mrx.swapcase() print(ample)

Apply the non-alphabetic characters: Example: mrx = "!!h@pPy bIRth d@y h@rry!!" ample = mrx.swapcase() print(ample)

Python Strings Method title()

The title() method generates a string with the first character in every word uppercase. It’s like a header or a title.

Words including numbers or symbols will be transformed into uppercase after the first letter.

Syntax string.title()

Parameters are not required – Make sure every word begins with an uppercase letter:

Example: 1 mrx = "guido van rossum is the founder of Python language" ample = mrx.title() print(ample)

Verify that every word begins with an uppercase letter in the following example: mrx = "GUIDO VAN ROSSUM is the founder of PYTHON language" ample = mrx.title() print(ample)

Ensure that the initial letter of each word is uppercase: mrx = "**Python is the 1st easy to learn language in the world**" ample = mrx.title() print(ample)

In the following example, check that the initial letter after a non-alphabet letter is changed to uppercase:

Example:

1

Python Strings Method translate()

Translate() generates a string having some chosen characters substituted with the character stored in a dictionary or mapping table.

To make a mapping table, call the maketrans() method.

In the absence of a dictionary/table, a character will not be substituted.

In place of characters, you must utilize ASCII codes when referring to a dictionary.

Syntax string.translate(table)

Parameter Values

Parameter Description table This is essential. Explains how to substitute a value in a dictionary or mapping table.

If there is a “K” letter, substitute it with a “J” letter:

Example:

#Utilize a dictionary with ASCII codes to substitute 75 (K) with 74 (J): mrx_dict = {75: 74} ample = "Greetings Kane.." print(ample.translate(mrx_dict))

Substitute “K” with “J” through a mapping table: mrx = "Greetings Kane.." ample_table = mrx.maketrans("K", "J") print(mrx.translate(ample_table))

To substitute multiple characters, utilize a mapping table: mrx = "Eva" ample1 = "aEv" ample2 = "yAm" mrx_table = mrx.maketrans(ample1, ample2) print(mrx.translate(mrx_table))

You can delete characters from the string through the third parameter in the mapping table: Example: mrx = "Bonjour Eva!!" ample1 = "Eva" ample2 = "Lia" ample3 = "Bonjour" mrx_table = mrx.maketrans(ample1, ample2, ample3) print(mrx.translate(mrx_table))

Utilizing a dictionary rather than a mapping table, here is the same example as above: mrx = "Bonjour Eva!!" ample_dict = {69: 76, 118: 105, 97: 97, 66: None, 111: None, 110: None, 106: None, 117: None, 114: None} print(mrx translate(ample_dict))

Python Strings Method upper()

upper() method generates a string that contains all letters in capital case.

Numbers and symbols are not taken into account.

Syntax string upper()

There are no parameters – Make the string larger case: mrx = "Welcome to PYTHON Strings upper() Method." ample = mrx.upper() print(ample)

Apply the digits at the beginning of the string then convert the string to capital case: mrx = "3.11.1 is the latest version of python!" ample = mrx.upper() print(ample)

Python Strings Method zfill()

The zfill() method inserts zeros (0) at the start of a string until it reaches the given length.

There is no filling if len is smaller than the length of the string.

Syntax string.zfill(len)

Parameter Values

Parameter Description len This is necessary. The length of the string should be represented by a number

To make the string 6 characters wide, fill it with zeros: mrx = "88" ample = mrx.zfill(6) print(ample)

Implement the zfill() method to string then utilize upper() method: mrx = "mrexamples" ample = mrx.zfill(15) ample = ample.upper() print(ample)

To make the strings various characters wide, fill them with zeros:

This article is from: