Author name: Vikram Chiluka

Python String maketrans() Method with Examples

In the previous article, we have discussed Python String isupper() Method Examples
maketrans() Method in Python:

The string maketrans() method returns a translation mapping table that can be used by the translate() method.

To put it simply, the maketrans() method is a static method that generates a one-to-one mapping of a character to its translation/replacement.

Each character is given a Unicode representation for translation.

When the translate() method is called, this translation mapping is used to replace a character with its mapped character.

Syntax:

string.maketrans(x, y, z)

Parameters:

x: If there is only one argument, it must be a dictionary.
A 1-to-1 mapping from a single character string to its translation OR a Unicode number (97 for ‘a’) to its translation should be included in the dictionary.

y: If two arguments are passed, they must be of equal length.
Each character in the first string is a replacement for the index in the second string that corresponds to it.

z: If three arguments are passed, the third argument’s characters are mapped to None.

Return Value:

The maketrans() method returns a translation table that contains a one-to-one mapping of a Unicode ordinal to its translation/replacement.

Examples:

Example1:

maketrans() Translation table using a dictionary
gvn_dict = {"a": "100", "b": "200", "c": "300"}
gvn_str = "abc"
print(gvn_str.maketrans(gvn_dict))

gvn_dict = {97: "100", 98: "200", 99: "300"}
gvn_str = "abc"
print(gvn_str.maketrans(gvn_dict))

Output:

{97: '100', 98: '200', 99: '300'}
{97: '100', 98: '200', 99: '300'}

Explanation:

A dictionary gvn_dict is defined here. It has a mapping of the characters a,b, and c to 100,200, and 300, respectively.

maketrans() generates a mapping from the character’s Unicode ordinal to its translation.

As a result, 97 (‘a’) corresponds to ‘100,’ 98 ‘b’ corresponds to 200, and 99 ‘c’ corresponds to 300. The output of both dictionaries demonstrates this.

In addition, if two or more characters are mapped in the dictionary, an exception is raised.

Example2:

maketrans() Translation table using two strings
gvn_fststr = "abc"
gvn_scndstr = "def"
gvn_str = "abc"
print(gvn_str.maketrans(gvn_fststr, gvn_scndstr))

gvn_fststr = "abc"
gvn_scndstr = "defghi"
gvn_str = "abc"
print(gvn_str.maketrans(gvn_fststr, gvn_scndstr))

Output:

Traceback (most recent call last):
  File "/home/d57f28dec313688f30b673716d7d7ec8.py", line 9, in <module>
    print(gvn_str.maketrans(gvn_fststr, gvn_scndstr))
ValueError: the first two maketrans arguments must have equal length

Explanation:

First, two strings of equal length are defined: abc and def. The corresponding translation is then generated.

Only printing the first translation results in a 1-to-1 mapping from each character’s Unicode ordinal in the first string to the same indexed character in the second string.

In this case, 97 (‘a’) corresponds to 100 (‘d’), 98 (‘b’) corresponds to 101 (‘e’), and 99 (‘c’) corresponds to 102 (‘f’).

Attempting to create a translation table for strings of unequal length raises a ValueError exception, indicating that the strings must be of equal length.

Example3:

maketrans() Translation table with removable string
gvn_fststr = "abc"
gvn_scndstr = "def"
gvn_thrdstr = "abd"
gvn_str = "abc"
print(gvn_str.maketrans(gvn_fststr, gvn_scndstr, gvn_thrdstr))

Output:

{97: None, 98: None, 99: 102, 100: None}

Explanation:

The mapping between the two strings firstString and secondString is created first.

The third argument, thirdString, then resets each character’s mapping to None and creates a new mapping for non-existent characters.

In this case, thirdString resets the mappings of 97 (‘a’) and 98 (‘b’) to None, as well as creating a new mapping for 100 (‘d’) that is also mapped to None.

Go through our tutorial and learn about various Python String Method Examples and learn how to apply the knowledge while dealing with strings.

Python String maketrans() Method with Examples Read More »

Python Program for Pass Statement with Examples

pass Statement in Python:

The pass statement serves as a placeholder for additional code.

The pass statement is a null statement in Python programming. The difference between a comment and a pass statement in Python is that while a comment is completely ignored by the interpreter, a pass statement is not.

When the pass is executed, however, nothing happens. It has no effect on the operation (NOP).

Assume we have a loop or a function that has not yet been implemented but that we intend to do so in the future. They cannot exist with an empty body. The interpreter would return an error message. As a result, we use the pass statement to create a body that does nothing.

Syntax:

pass
In a function definition, use the pass keyword as follows:

Example:

# Without the pass statement, having an empty function definition like this
# would result in an error.
def empty_function():
    pass

Output:

Explanation:

we don't get any output since it is an empty function.But, Without the pass 
statement, having an empty function definition like this would 
result in an error.
In a class definition, use the pass keyword as follows:

Example:

# Without the pass statement, having an empty class definition like this 
# would result in an error.
class Person:
  pass

In loops, use the pass keyword as follows:

Example:

gvn_seqnce = {'a', 'b', 'c', 'd'}
for itr in gvn_seqnce:
    pass

Program for pass Statement in Python

Method #1: Using Built-in Functions (Static Input)

In an, if statement, use the pass keyword as follows:

Approach:

  • Give the first number as static input and store it in a variable.
  • Give the second number as static input and store it in another variable.
  • Check if the given second number is greater than the first number using the if conditional statement.
  • If it is true, then give a pass statement.
  • Else print the given first and second numbers.
  • The Exit of the Program.

Below is the implementation:

# Give the first number as static input and store it in a variable.
numb1 = 33
# Give the second number as static input and store it in another variable.
numb2 = 50
# Check if the given second number is greater than the first number using
# the if conditional statement.
if numb2 > numb1:
    # If it is true, then give a pass statement.
    pass
else:
    # Else print the given first and second numbers.
    print(numb1)
    print(numb2)

Output:

Explanation:

Here, We don't get any output as we gave a pass statement if the condition
satisfies.

Method #2: Using Built-in Functions (User Input)

Approach:

  • Give the first number as user input using the int(input()) and store it in a variable.
  • Give the second number as user input using the int(input()) and store it in another variable.
  • Check if the given second number is greater than the first number using the if conditional statement.
  • If it is true, then give a pass statement.
  • Else print the given first and second numbers.
  • The Exit of the Program.

Below is the implementation:

# Give the first number as user input using the int(input()) and store it in a variable.
numb1 = int(input("Enter some random number = "))
# Give the second number as user input using the int(input()) and store it in another variable.
numb2 = int(input("Enter some random number = "))
# Check if the given second number is greater than the first number using
# the if conditional statement.
if numb2 > numb1:
    # If it is true, then give a pass statement.
    pass
else:
    # Else print the given first and second numbers.
    print(numb1)
    print(numb2)

Output:

Enter some random number = 100
Enter some random number = 20
100
20

Find a Comprehensive Collection of Python Built in Functions that you need to be aware of and use them as a part of your program.

Python Program for Pass Statement with Examples Read More »

Python List copy() Method with Examples

In the previous article, we have discussed Python List clear() Method with Examples
List in Python:

Lists in Python are mutable sequences. They are extremely similar to tuples, except they do not have immutability constraints. Lists are often used to store collections of homogeneous things, but there is nothing stopping you from storing collections of heterogeneous items as well.

List copy() Method in Python:

The copy() method returns a duplicate (copy) of the specified list.

Syntax:

list.copy()

Parameter Values: This method has no parameters.

Return Value:

A new list is returned by the copy() function. It makes no changes to the original list.

Examples:

Example1:

Input:

Given List = ['hello', 'this', 'is', 'btechgeeks']

Output:

The given list is :  ['hello', 'this', 'is', 'btechgeeks']
The new list after applying copy() function is :
['hello', 'this', 'is', 'btechgeeks']

Example2:

Input:

Given List = [20, 40, 60, 80, 100]

Output:

The given list is :  [20, 40, 60, 80, 100]
The new list after applying copy() function is :
[20, 40, 60, 80, 100]

List copy() Method with Examples in Python

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Give the list as static input and store it in a variable.
  • Print the given list.
  • Copy all the elements of the given list into the new list using the copy() function and store it in another variable.
  • Print the above new list which is the duplicate of the given list.
  • The Exit of Program.

Below is the implementation:

# Give the list as static input and store it in a variable.
gvn_lst = ["hello", "this", "is", "btechgeeks"]
# Print the given list.
print("The given list is : ", gvn_lst)
# Copy all the elements of the given list into the new list using the copy()
# function and store it in another variable.
new_lst = gvn_lst.copy()
# Print the above new list which is the duplicate of the given list.
print("The new list after applying copy() function is :")
print(new_lst)

Output:

The given list is :  ['hello', 'this', 'is', 'btechgeeks']
The new list after applying copy() function is :
['hello', 'this', 'is', 'btechgeeks']

Method #2: Using Built-in Functions (User Input)

Approach:

  • Give the list as user input using list(),map(),input(),and split() functions.
  • Store it in a variable.
  • Print the given list.
  • Copy all the elements of the given list into the new list using the copy() function and store it in another variable.
  • Print the above new list which is the duplicate of the given list.
  • The Exit of Program.

Below is the implementation:

# Give the list as user input using list(),map(),input(),and split() functions.
# Store it in a variable.
gvn_lst = list(map(int, input(
   'Enter some random List Elements separated by spaces = ').split()))
# Print the given list.
print("The given list is : ", gvn_lst)
# Copy all the elements of the given list into the new list using the copy()
# function and store it in another variable.
new_lst = gvn_lst.copy()
# Print the above new list which is the duplicate of the given list.
print("The new list after applying copy() function is :")
print(new_lst)

Output:

Enter some random List Elements separated by spaces = 20 40 60 80 100
The given list is : [20, 40, 60, 80, 100]
The new list after applying copy() function is :
[20, 40, 60, 80, 100]

copying List using ‘=’ Operator

To copy a list, we can also use the = operator.

However, there is one drawback to copying lists in this manner. When you change a new list, you also change the old list. It’s because the new list refers to or points to the same old list object.

Example:

Approach:

  • Give the list as static input and store it in a variable.
  • Copy the elements of the given list into the new list using the ‘=’ operator.
  • Store it in another variable.
  • Add an element to the new list using the append() function.
  • Print the given original list.
  • Print the new list obtained.
  • The Exit of Program.

Below is the implementation:

# Give the list as static input and store it in a variable.
gvn_lst = [20, 40, 60, 80, 100]
# copy the elements of the given list into new list using the '=' operator
# store it in another variable.
new_lst = gvn_lst
# Add an element to the new list using the append() function
new_lst.append('python')
# Print the given original list.
print("The given original list is:", gvn_lst)
# Print the new list obtained.
print("The new list obtained is :", new_lst)

Output:

The given original list is: [20, 40, 60, 80, 100, 'python']
The new list obtained is : [20, 40, 60, 80, 100, 'python']

copying List using Slicing

For Example:

# Give the list as static input and store it in a variable.
gvn_lst = ['good', 'morning', 'btechgeeks', 123]
# copy the elements of the given list into new list using the slicing.
# store it in another variable.
new_lst = gvn_lst[:]
# Add an element to the new list using the append() function
new_lst.append(1000)
# Print the given original list.
print("The given original list is:", gvn_lst)
# Print the new list obtained.
print("The new list obtained is :", new_lst)

Output:

The given original list is: ['good', 'morning', 'btechgeeks', 123]
The new list obtained is : ['good', 'morning', 'btechgeeks', 123, 1000]

Know about the syntax and usage of functions to manipulate lists with the provided Python List Method Examples and use them.

Python List copy() Method with Examples Read More »

Python List clear() Method with Examples

In the previous article, we have discussed Python List append() Method with Examples
List in Python:

Lists in Python are mutable sequences. They are extremely similar to tuples, except they do not have immutability constraints. Lists are often used to store collections of homogeneous things, but there is nothing stopping you from storing collections of heterogeneous items as well.

List clear() Method in Python:

The clear() method clears or removes all the elements from the list.

Syntax:

list.clear()

Parameter Values: This method has no parameters.

Return Value:

The clear() method only removes items from the specified list. It doesn’t return anything.

Examples:

Example1:

Input:

Given list = ["good", "morning", "btechgeeks"]

Output:

The given list is : ['good', 'morning', 'btechgeeks']
The given list after applying clear() method: []

Example2:

Input:

Given list = [25, 35, 67, 12, 15]

Output:

The given list is : [25, 35, 67, 12, 15]
The given list after applying clear() method: []

List clear() Method with Examples in Python

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Give the list as static input and store it in a variable.
  • Print the above-given list.
  • Applying the clear() method to the given list that clears or removes all the elements from the given list.
  • Print the given list after removing all its elements.
  • The Exit of Program.

Below is the implementation:

# Give the list as static input and store it in a variable.
gvn_lst = ["good", "morning", "btechgeeks"]
# Print the above-given list.
print("The given list is :", gvn_lst)
# Applying the clear() method to the given list that clears or removes all the
# elements from the given list.
gvn_lst.clear()
# Print the given list after removing all its elements.
print("The given list after applying clear() method:", gvn_lst)

Output:

The given list is : ['good', 'morning', 'btechgeeks']
The given list after applying clear() method: []

Method #2: Using Built-in Functions (User Input)

Approach:

  • Give the list as user input using list(),map(),input(),and split() functions.
  • Store it in a variable.
  • Print the above-given list.
  • Applying the clear() method to the given list that clears or removes all the elements from the given list.
  • Print the given list after removing all its elements.
  • The Exit of Program.

Below is the implementation:

# Give the list as user input using list(),map(),input(),and split() functions.
# Store it in a variable.
gvn_lst = list(map(int, input(
   'Enter some random List Elements separated by spaces = ').split()))
# Print the above-given list.
print("The given list is :", gvn_lst)
# Applying the clear() method to the given list that clears or removes all the
# elements from the given list.
gvn_lst.clear()
# Print the given list after removing all its elements.
print("The given list after applying clear() method:", gvn_lst)

Output:

Enter some random List Elements separated by spaces = 2 4 6 9 1
The given list is : [2, 4, 6, 9, 1]
The given list after applying clear() method: []

Note: You cannot use the clear() method if you are using Python 2 or Python 3.2 or lower. Instead, use the del operator.

Using del to clear the list

Approach:

  • Give the list as static input and store it in a variable.
  • Print the above-given list.
  • Clear all the elements of the given list using the del operator.
  • Print the given list after removing all its elements.
  • The Exit of Program.

Below is the implementation:

# Give the list as static input and store it in a variable.
gvn_lst = [50, 100, 150, 200]
# Print the above-given list.
print("The given list is :", gvn_lst)
# Clear all the elements of the given list using the del operator.
del gvn_lst[:]
# Print the given list after removing all its elements.
print("The given list after applying del operator:", gvn_lst)

Output:

The given list is : [50, 100, 150, 200]
The given list after applying del operator: []

Know about the syntax and usage of functions to manipulate lists with the provided Python List Method Examples and use them.

Python List clear() Method with Examples Read More »

Python List append() Method with Examples

In the previous article, we have discussed Python String islower() Method with Examples
List in Python:

Lists in Python are mutable sequences. They are extremely similar to tuples, except they do not have immutability constraints. Lists are often used to store collections of homogeneous things, but there is nothing stopping you from storing collections of heterogeneous items as well.

List append() Method in Python:

The append() method adds an element or item to the end of the list.

Syntax:

list.append(item)

Parameters

The method only accepts one argument.

item – an item (number, string, list, etc.) that will be appended to the end of the list

Return Value:

The append() method produces no output (returns None).

Examples:

Example1:

Input:

Given list = ["hello", "this", "is"]
Item to be added = "btechgeeks"

Output:

The given list after appending { btechgeeks } =  ['hello', 'this', 'is', 'btechgeeks']

Example2:

Input:

Given list = [10, 15, 20, 25]
Item to be added = 30

Output:

The given list after appending { 30 } = [10, 15, 20, 25, 30]

List append() Method with Examples in Python

Method #1: Using Built-in Functions (Static Input)

1)Appending an item to List

Approach:

  • Give the list as static input and store it in a variable.
  • Give the string to be added as static input and store it in another variable.
  • Append the above-given string item to the given list using the append() function.
  • Print the given list after appending(adding) the given string item.
  • The Exit of Program.

Below is the implementation:

# Give the list as static input and store it in a variable.
gvn_lst = ["hello", "this", "is"]
# Give the string to be added as static input and store it in another variable.
new_item = "btechgeeks"
# Append the above-given string item to the given list using the append()
# function.
gvn_lst.append(new_item)
# Print the given list after appending(adding) the given string item.
print("The given list after appending {", new_item, "} = ", gvn_lst)

Output:

The given list after appending { btechgeeks } =  ['hello', 'this', 'is', 'btechgeeks']
2)Appending List to a List

Approach:

  • Give the list as static input and store it in a variable.
  • Give the new list to be added as static input and store it in another variable.
  • Append the above-given new list to the given list using the append() function.
  • Print the given list after appending the given new list.
  • The Exit of Program.

Below is the implementation:

# Give the list as static input and store it in a variable.
gvn_lst = ["good", "morning", "btechgeeks"]
# Give the new list to be added as static input and store it in another variable.
new_lst = [1, 2, 3, 4, 5]
# Append the above-given new list to the given list using the append()
# function.
gvn_lst.append(new_lst)
# Print the given list after appending the given new list.
print("The given list after appending new list ", new_lst, ":")
print(gvn_lst)

Output:

The given list after appending new list  [1, 2, 3, 4, 5] :
['good', 'morning', 'btechgeeks', [1, 2, 3, 4, 5]]
Method #2: Using Built-in Functions (User Input)
1)Appending an item to List

Approach:

  • Give the list as user input using list(),map(),input(),and split() functions.
  • Store it in a variable.
  • Give the item to be added as user input using the int(input()) function and store it in another variable.
  • Append the above-given item to the given list using the append() function.
  • Print the given list after appending(adding) the given item.
  • The Exit of Program.

Below is the implementation:

# Give the list as user input using list(),map(),input(),and split() functions.
# Store it in a variable.
gvn_lst = list(map(int, input(
   'Enter some random List Elements separated by spaces = ').split()))
# Give the item to be added as user input using the int(input()) function 
# and store it in another variable.
new_item = int(input("Enter some random number = "))
# Append the above-given item to the given list using the append()
# function.
gvn_lst.append(new_item)
# Print the given list after appending(adding) the given item.
print("The given list after appending {", new_item, "} = ", gvn_lst)

Output:

Enter some random List Elements separated by spaces = 10 15 20 25
Enter some random number = 30
The given list after appending { 30 } = [10, 15, 20, 25, 30]
2)Appending List to a List

Approach:

  • Give the list as user input using list(),map(),input(),and split() functions.
  • Store it in a variable.
  • Give the new list to be added as user input using list(),map(),input(),and split() functions.
  • Store it in another variable.
  • Append the above-given new list to the given list using the append() function.
  • Print the given list after appending the given new list.
  • The Exit of Program.

Below is the implementation:

# Give the list as user input using list(),map(),input(),and split() functions.
# Store it in a variable.
gvn_lst = list(map(int, input(
   'Enter some random List Elements separated by spaces = ').split()))
# Give the new list to be added as user input using list(),map(),input(),and split() functions.
# Store it in another variable.
new_lst = list(map(int, input(
   'Enter some random List Elements separated by spaces = ').split()))
# Append the above-given new list to the given list using the append()
# function.
gvn_lst.append(new_lst)
# Print the given list after appending the given new list.
print("The given list after appending new list ", new_lst, ":")
print(gvn_lst)

Output:

Enter some random List Elements separated by spaces = 100 200 300
Enter some random List Elements separated by spaces = 400 500 600
The given list after appending new list [400, 500, 600] :
[100, 200, 300, [400, 500, 600]]

 

Python List append() Method with Examples Read More »

Python String islower() Method with Examples

In the previous article, we have discussed Python String zfill() Method with Examples
islower() Method in Python:

If all of the characters are in lower case, the islower() method returns True; otherwise, it returns False.

Only alphabet characters are checked, not numbers, symbols, or spaces.

Syntax:

string.islower()

Parameters: This function has no parameters.

Return Value:

The islower() method gives:

  • True if all of the alphabets in the string are lowercase.
  • If the string contains at least one uppercase alphabet, this function returns False.

Examples:

Example1:

Input:

Given string = 'welcome to python-programs'
Given string = '1234 Hello ALL @'
Given string = 'good morning btechgeeks 12345 @#!'

Output:

The given string =  welcome to python-programs
Checking if the given string has all lowercase characters or not =  True
The given string =  1234 Hello ALL @
Checking if the given string has all lowercase characters or not =  False
The given string =  good morning btechgeeks 12345 @#!
Checking if the given string has all lowercase characters or not =  True

Example2:

Input:

Given string = 'hello this is btech_geeks'
Given string = 'ThIs IS a PYThon learning PLATform'
Given string = '#### hello everyone ##%%$12346'

Output:

The given string =  hello this is btech_geeks
Checking if the given string has all lowercase characters or not =  True
The given string =  ThIs IS a PYThon learning PLATform
Checking if the given string has all lowercase characters or not =  False
The given string =  #### hello everyone ##%%$12346
Checking if the given string has all lowercase characters or not =  True

String islower() Method Examples in Python

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Give the string as static input and store it in a variable.
  • Apply islower() function to the given string in which if all of the characters are in lower case, the islower() method returns True; otherwise, it returns False.
  • Store it in another variable.
  • Print the above-given String.
  • Print the result after checking if the given string has all lowercase characters or not.
  • Similarly, do the same for other strings and print the result.
  • The Exit of Program.

Below is the implementation:

# Give the string as static input and store it in a variable.
gvn_strng1 = 'welcome to python-programs'
# Apply islower() function to the given string in which if all of the characters
# are in lower case, the islower() method returns True; otherwise, it returns False.
# Store it in another variable.
rslt_1 = gvn_strng1.islower()
# Print the above-given String.
print("The given string = ", gvn_strng1)
# Print the result after checking if the given string has all lowercase
# characters or not.
print("Checking if the given string has all lowercase characters or not = ", rslt_1)
# Similarly, do the same for other strings and print the result.
gvn_strng2 = '1234 Hello ALL @'
rslt_2 = gvn_strng2.islower()
print("The given string = ", gvn_strng2)
print("Checking if the given string has all lowercase characters or not = ", rslt_2)

gvn_strng3 = 'good morning btechgeeks 12345 @#!'
rslt_3 = gvn_strng3.islower()
print("The given string = ", gvn_strng3)
print("Checking if the given string has all lowercase characters or not = ", rslt_3)

Output:

The given string =  welcome to python-programs
Checking if the given string has all lowercase characters or not =  True
The given string =  1234 Hello ALL @
Checking if the given string has all lowercase characters or not =  False
The given string =  good morning btechgeeks 12345 @#!
Checking if the given string has all lowercase characters or not =  True

Method #2: Using Built-in Functions (User Input)

Approach:

  • Give the string as user input using the input() function and store it in a variable.
  • Apply islower() function to the given string in which if all of the characters are in lower case, the islower() method returns True; otherwise, it returns False.
  • Store it in another variable.
  • Print the above-given String.
  • Print the result after checking if the given string has all lowercase characters or not.
  • Similarly, do the same for other strings and print the result.
  • The Exit of Program.

Below is the implementation:

# Give the string as user input using the input() function and store it in a variable.
gvn_strng1 = input("Enter some Random String = ")
# Apply islower() function to the given string in which if all of the characters
# are in lower case, the islower() method returns True; otherwise, it returns False.
# Store it in another variable.
rslt_1 = gvn_strng1.islower()
# Print the above-given String.
print("The given string = ", gvn_strng1)
# Print the result after checking if the given string has all lowercase
# characters or not.
print("Checking if the given string has all lowercase characters or not = ", rslt_1)
# Similarly, do the same for other strings and print the result.
gvn_strng2 = input("Enter some Random String = ")
rslt_2 = gvn_strng2.islower()
print("The given string = ", gvn_strng2)
print("Checking if the given string has all lowercase characters or not = ", rslt_2)

gvn_strng3 = input("Enter some Random String = ")
rslt_3 = gvn_strng3.islower()
print("The given string = ", gvn_strng3)
print("Checking if the given string has all lowercase characters or not = ", rslt_3)

Output:

Enter some Random String = hello btechgeeks
The given string = hello btechgeeks
Checking if the given string has all lowercase characters or not = True
Enter some Random String = ThIS IS a pytHON learning PLATform
The given string = ThIS IS a pytHON learning PLATform
Checking if the given string has all lowercase characters or not = False
Enter some Random String = ###% hello everyone #$*
The given string = ###% hello everyone #$*
Checking if the given string has all lowercase characters or not = True

Go through our tutorial and learn about various Python String Method Examples and learn how to apply the knowledge while dealing with strings.

Python String islower() Method with Examples Read More »

Python String isnumeric() Method with Examples

In the previous article, we have discussed Python Program for help() Function with Examples
isnumeric() Method in Python:

If all of the characters are numeric (0-9), the isnumeric() method returns True; otherwise, it returns False.

Exponents such as ² and ¾ are also considered numeric values.

Because all of the characters in the string must be numeric, “-2” and “1.4” are NOT considered numeric values.

And the ‘-‘, ‘.’ are not the numeric values.

Numeric characters in Python include decimal characters (such as 0, 1, 2..), digits (such as subscript, superscript), and characters with the Unicode numeric value property (such as fractions, roman numerals, currency numerators).

Syntax:

string.isnumeric()

Parameters: This function has no parameters.

Return Value:

The isnumeric() method gives:

  • True if all of the characters in the string are numbers.
  • If at least one of the characters is not a numeric character, the statement is false.

Examples:

Example1:

Input:

Given string = '-6'
Given string = '5/6'
Given string = 'btechgeeks 12345'

Output:

The given string =  -6
Checking if the given string has all numeric characters or not =  False
The given string =  5/6
Checking if the given string has all numeric characters or not =  False
The given string =  btechgeeks 12345
Checking if the given string has all numeric characters or not =  False

Example2:

Input:

Given string = '\u0030'
Given string = '1.7'
Given string = '\u00B2'

Output:

The given string = 0
Checking if the given string has all numeric characters or not = True
The given string = 1.7
Checking if the given string has all numeric characters or not = False
The given string = ²
Checking if the given string has all numeric characters or not = True

String isnumeric() Method Examples in Python

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Give the string as static input and store it in a variable.
  • Apply isnumeric() function to the given string in which If all of the characters are numeric (0-9), the isnumeric() method returns True; otherwise, it returns False.
  • Store it in another variable.
  • Print the above-given String.
  • Print the result after checking if the given string has all numeric characters or not.
  • Similarly, do the same for other strings and print the result.
  • The Exit of Program.

Below is the implementation:

# Give the string as static input and store it in a variable.
# unicode for the number 0
gvn_strng1 = '\u0030'
# Apply isnumeric() function to the given string in which If all of the
# characters are numeric (0-9), the isnumeric() method returns True;
# otherwise, it returns False.
# Store it in another variable.
rslt_1 = gvn_strng1.isnumeric()
# Print the above-given String.
print("The given string = ", gvn_strng1)
# Print the result after checking if the given string has all numeric
# characters or not.
print("Checking if the given string has all numeric characters or not = ", rslt_1)
# Similarly, do the same for other strings and print the result.
gvn_strng2 = '1.7'
rslt_2 = gvn_strng2.isnumeric()
print("The given string = ", gvn_strng2)
print("Checking if the given string has all numeric characters or not = ", rslt_2)
# unicode for the value ²
gvn_strng3 = '\u00B2'
rslt_3 = gvn_strng3.isnumeric()
print("The given string = ", gvn_strng3)
print("Checking if the given string has all numeric characters or not = ", rslt_3)

Output:

The given string = 0
Checking if the given string has all numeric characters or not = True
The given string = 1.7
Checking if the given string has all numeric characters or not = False
The given string = ²
Checking if the given string has all numeric characters or not = True

Method #2: Using Built-in Functions (User Input)

Approach:

  • Give the string as user input using the input() function and store it in a variable.
  • Apply isnumeric() function to the given string in which If all of the characters are numeric (0-9), the isnumeric() method returns True; otherwise, it returns False.
  • Store it in another variable.
  • Print the above-given String.
  • Print the result after checking if the given string has all numeric characters or not.
  • Similarly, do the same for other strings and print the result.
  • The Exit of Program.

Below is the implementation:

# Give the string as user input using the input() function and store it in a variable.
gvn_strng1 =  input("Enter some Random String = ")
# Apply isnumeric() function to the given string in which If all of the
# characters are numeric (0-9), the isnumeric() method returns True;
# otherwise, it returns False.
# Store it in another variable.
rslt_1 = gvn_strng1.isnumeric()
# Print the above-given String.
print("The given string = ", gvn_strng1)
# Print the result after checking if the given string has all numeric
# characters or not.
print("Checking if the given string has all numeric characters or not = ", rslt_1)
# Similarly, do the same for other strings and print the result.
gvn_strng2 =  input("Enter some Random String = ")
rslt_2 = gvn_strng2.isnumeric()
print("The given string = ", gvn_strng2)
print("Checking if the given string has all numeric characters or not = ", rslt_2)
gvn_strng3 = input("Enter some Random String = ")
rslt_3 = gvn_strng3.isnumeric()
print("The given string = ", gvn_strng3)
print("Checking if the given string has all numeric characters or not = ", rslt_3)

Output:

Enter some Random String = -10.5
The given string = -10.5
Checking if the given string has all numeric characters or not = False
Enter some Random String = 5/6
The given string = 5/6
Checking if the given string has all numeric characters or not = False
Enter some Random String = btechgeeks 12345
The given string = btechgeeks 12345
Checking if the given string has all numeric characters or not = False

Go through our tutorial and learn about various Python String Method Examples and learn how to apply the knowledge while dealing with strings.

Python String isnumeric() Method with Examples Read More »

Python String zfill() Method with Examples

In the previous article, we have discussed Python String splitlines() Method with Examples
zfill() Method in Python:

The zfill() method appends zeros (0) to the beginning of a string until it reaches the specified length.

If the len parameter value is less than the length of the string, no filling is performed.

Syntax:

string.zfill(len)

Parameters

len: This is Required. A number indicating the string’s desired length.

Return Value:

zfill() returns a copy of the string with 0 to the left filled in. The length of the returned string is determined by the width specified.

  • Assume the string’s initial length is 10. Furthermore, the width is specified as 15. zfill() returns a copy of the string with five ‘0’ digits filled to the left in this case.
  • Assume the string’s initial length is 10. In addition, the width is specified as 8. In this case, zfill() returns a copy of the original string rather than filling ‘0’ digits to the left. In this case, the length of the returned string will be 10.

Examples:

Example1:

Input:

Given string = "hello btechgeeks"
Given number = 10

Output:

hello btechgeeks

Example2:

Input:

Given string = "hello btechgeeks"
Given number = 25

Output:

000000000hello btechgeeks

String zfill() Method with Examples in Python

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Give the first string as static input and store it in a variable.
  • Give the number as static input and store it in another variable.
  • Apply zfill() function to the given first string by passing the above-given number as an argument that Fills the string with zeros until it reaches a length of a given number of characters.
  • Store it in another variable.
  • Print the above result.
  • The Exit of Program.

Below is the implementation:

# Give the first string as static input and store it in a variable.
gvn_fststr = "35"
# Give the number as static input and store it in another variable.
gvn_numb1 = 10
# Apply zfill() function to the given first string by passing the above-given
# number as an argument that Fills the string with zeros until it reaches a
# length of given number of characters.
# Store it in another variable.
rslt = gvn_fststr.zfill(gvn_numb1)
# Print the above result.
print(rslt)

Output:

0000000035

Similarly, do the same for the other strings and print the result.

# Similarly, do the same for the other strings and print the result.
gvn_scndstr = "hello btechgeeks"
gvn_numb2 = 20
print(gvn_scndstr.zfill(gvn_numb2))

Output:

0000hello btechgeeks
gvn_thrdstr = "20.55"
gvn_numb3 = 8
print(gvn_thrdstr.zfill(gvn_numb3))

Output:

00020.55

Method #2: Using Built-in Functions (User Input)

Approach:

  • Give the first string as user input using the input() function and store it in a variable.
  • Give the number as user input using the int(input()) function and store it in another variable.
  • Apply zfill() function to the given first string by passing the above-given number as an argument that Fills the string with zeros until it reaches a length of a given number of characters.
  • Store it in another variable.
  • Print the above result.
  • The Exit of Program.

Below is the implementation:

# Give the first string as user input using the input() function and store it in a variable.
gvn_fststr = input("Enter some Random String = ")
# Give the number as user input using the int(input()) function and store it in another variable.
gvn_numb1 = int(input("Enter some Random number = "))
# Apply zfill() function to the given first string by passing the above-given
# number as an argument that Fills the string with zeros until it reaches a
# length of given number characters.
# Store it in another variable.
rslt = gvn_fststr.zfill(gvn_numb1)
# Print the above result.
print("The result string = ", rslt)

Output:

Enter some Random String = good morning btechgeeks
Enter some Random number = 11
The result string = good morning btechgeeks

Similarly, do the same for other strings and print the result.

Below is the implementation:

# Similarly, do the same for other strings and print the result.
gvn_scndstr = input("Enter some Random String = ")
gvn_numb2 = int(input("Enter some Random number = "))
rslt = gvn_scndstr.zfill(gvn_numb2)
print("The result string = ", rslt)

Output:

Enter some Random String = 250.6
Enter some Random number = 6
The result string = 0250.6

Below is the implementation:

# Similarly, do the same for other strings and print the result.
gvn_thrdstr = input("Enter some Random String = ")
gvn_numb3 = int(input("Enter some Random number = "))
rslt = gvn_thrdstr.zfill(gvn_numb3)
print("The result string = ", rslt)

Output:

Enter some Random String = welcome to btechgeeks
Enter some Random number = 27
The result string = 000000welcome to btechgeeks

Go through our tutorial and learn about various Python String Method Examples and learn how to apply the knowledge while dealing with strings.

Python String zfill() Method with Examples Read More »

Python String isupper() Method with Examples

In the previous article, we have discussed Python String istitle() Method Examples
isupper() Method in Python:

The string isupper() method determines whether all characters in a string are uppercased.

Syntax:

string.isupper()

Parameters: This function has no parameters.

Return Value:

The isupper() method gives:

  • True if all of the characters in a string are uppercase.
  • If any of the characters in a string are lowercase, this function returns false.

Examples:

Example1:

Input:

Given string = 'Welcome To Python-programs'
Given string = '1234 HELLO ALL @'
Given string = 'GOOD MORNING BTECHGEEKS'

Output:

The given string =  Welcome To Python-programs
Checking if the given string has all uppercase characters or not =  False
The given string =  1234 HELLO ALL @
Checking if the given string has all uppercase characters or not =  True
The given string =  GOOD MORNING BTECHGEEKS
Checking if the given string has all uppercase characters or not =  True

Example2:

Input:

Given string = 'HELLO THIS IS BTECH_GEEKS'
Given string = 'ThIs IS a PYThon learning PLATform'
Given string = '#### HELLO EVERYONE ##%%$12346'

Output:

The given string =  HELLO THIS IS BTECH_GEEKS
Checking if the given string has all uppercase characters or not =  True
The given string =  ThIs IS a PYThon learning PLATform
Checking if the given string has all uppercase characters or not =  False
The given string =  #### HELLO EVERYONE ##%%$12346
Checking if the given string has all uppercase characters or not =  True

String isupper() Method Examples in Python

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Give the string as static input and store it in a variable.
  • Apply isupper() function to the given string that returns true if all of the characters in a given string are uppercase. If any of the characters in a string are lowercase, this function returns false.
  • Store it in another variable.
  • Print the above-given String.
  • Print the result after checking if the given string has all uppercase characters or not.
  • Similarly, do the same for other strings and print the result.
  • The Exit of Program.

Below is the implementation:

# Give the string as static input and store it in a variable.
gvn_strng1 = 'Welcome To Python-programs'
# Apply isupper() function to the given string that returns true if all
# of the characters in a given string are uppercase. If any of the characters
# in a string are lowercase, this function returns false.
# Store it in another variable.
rslt_1 = gvn_strng1.isupper()
# Print the above-given String.
print("The given string = ", gvn_strng1)
# Print the result after checking if the given string has all uppercase
# characters or not.
print("Checking if the given string has all uppercase characters or not = ", rslt_1)
# Similarly, do the same for other strings and print the result.
gvn_strng2 = '1234 HELLO ALL @'
rslt_2 = gvn_strng2.isupper()
print("The given string = ", gvn_strng2)
print("Checking if the given string has all uppercase characters or not = ", rslt_2)

gvn_strng3 = 'GOOD MORNING BTECHGEEKS'
rslt_3 = gvn_strng3.isupper()
print("The given string = ", gvn_strng3)
print("Checking if the given string has all uppercase characters or not = ", rslt_3)

Output:

The given string =  Welcome To Python-programs
Checking if the given string has all uppercase characters or not =  False
The given string =  1234 HELLO ALL @
Checking if the given string has all uppercase characters or not =  True
The given string =  GOOD MORNING BTECHGEEKS
Checking if the given string has all uppercase characters or not =  True

Method #2: Using Built-in Functions (User Input)

Approach:

  • Give the string as user input using the input() function and store it in a variable.
  • Apply isupper() function to the given string that returns true if all of the characters in a given string are uppercase. If any of the characters in a string are lowercase, this function returns false.
  • Store it in another variable.
  • Print the above-given String.
  • Print the result after checking if the given string has all uppercase characters or not.
  • Similarly, do the same for other strings and print the result.
  • The Exit of Program.

Below is the implementation:

# Give the string as user input using the input() function and store it in a variable.
gvn_strng1 = input("Enter some Random String = ")
# Apply isupper() function to the given string that returns true if all
# of the characters in a given string are uppercase. If any of the characters
# in a string are lowercase, this function returns false.
# Store it in another variable.
rslt_1 = gvn_strng1.isupper()
# Print the above-given String.
print("The given string = ", gvn_strng1)
# Print the result after checking if the given string has all uppercase
# characters or not.
print("Checking if the given string has all uppercase characters or not = ", rslt_1)
# Similarly, do the same for other strings and print the result.
gvn_strng2 = input("Enter some Random String = ")
rslt_2 = gvn_strng2.isupper()
print("The given string = ", gvn_strng2)
print("Checking if the given string has all uppercase characters or not = ", rslt_2)

gvn_strng3 = input("Enter some Random String = ")
rslt_3 = gvn_strng3.isupper()
print("The given string = ", gvn_strng3)
print("Checking if the given string has all uppercase characters or not = ", rslt_3)

Output:

Enter some Random String = HELLO THIS IS BTECH_GEEKS
The given string = HELLO THIS IS BTECH_GEEKS
Checking if the given string has all uppercase characters or not = True
Enter some Random String = THis is A PYTHON LEARNING PLATform
The given string = THis is A PYTHON LEARNING PLATform
Checking if the given string has all uppercase characters or not = False
Enter some Random String = #### HELLO EVERYONE #@@#5678
The given string = #### HELLO EVERYONE #@@#5678
Checking if the given string has all uppercase characters or not = True

Go through our tutorial and learn about various Python String Method Examples and learn how to apply the knowledge while dealing with strings.

Python String isupper() Method with Examples Read More »

Python String istitle() Method with Examples

In the previous article, we have discussed Python String isspace() Method Examples
istitle() Method in Python:

If the string is titlecased, the istitle() function returns True. If it does not, it returns False.

Syntax:

string.istitle()

Parameters: This function has no parameters.

Return Value:

The istitle() method gives:

  • True if the string is titlecased.
  • If the string is not titlecased or is empty, it returns False.

Examples:

Example1:

Input:

Given string = 'Welcome To Python-Programs'
Given string = 'good morning btechgeeks'
Given string = 'Hello All @ This Is Btechgeeks'

Output:

The given string =  Welcome To Python-Programs
Checking if the given string is titlecased or not =  True
The given string =  good morning btechgeeks
Checking if the given string is titlecased or not =  False
The given string =  Hello All @ This Is Btechgeeks
Checking if the given string is titlecased or not =  True

Example2:

Input:

Given string = 'Welcome To Python-programs'
Given string = '12345 Hello All'
Given string = 'GOOD MORNING BTECHGEEKS'

Output:

The given string =  Welcome To Python-programs
Checking if the given string is titlecased or not =  False
The given string =  12345 Hello All
Checking if the given string is titlecased or not =  True
The given string =  GOOD MORNING BTECHGEEKS
Checking if the given string is titlecased or not =  False

String istitle() Method Examples in Python

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Give the string as static input and store it in a variable.
  • Apply istitle() function to the given string in which if the string is titlecased, the istitle() function returns True. If it does not, it returns False.
  • Store it in another variable.
  • Print the above-given String.
  • Print the result after checking if the given string is titlecased or not.
  • Similarly, do the same for other strings and print the result.
  • The Exit of Program.

Below is the implementation:

# Give the string as static input and store it in a variable.
gvn_strng1 = 'Welcome To Python-Programs'
# Apply istitle() function to the given string in which if the string
# is titlecased, the istitle() function returns True. If it does not,
# it returns False.
# Store it in another variable.
rslt_1 = gvn_strng1.istitle()
# Print the above-given String.
print("The given string = ", gvn_strng1)
# Print the result after checking if the given string is titlecased or not
print("Checking if the given string is titlecased or not = ", rslt_1)
# Similarly, do the same for other strings and print the result.
gvn_strng2 = 'good morning btechgeeks'
rslt_2 = gvn_strng2.istitle()
print("The given string = ", gvn_strng2)
print("Checking if the given string is titlecased or not = ", rslt_2)

gvn_strng3 = 'Hello All @ This Is Btechgeeks'
rslt_3 = gvn_strng3.istitle()
print("The given string = ", gvn_strng3)
print("Checking if the given string is titlecased or not = ", rslt_3)

Output:

The given string =  Welcome To Python-Programs
Checking if the given string is titlecased or not =  True
The given string =  good morning btechgeeks
Checking if the given string is titlecased or not =  False
The given string =  Hello All @ This Is Btechgeeks
Checking if the given string is titlecased or not =  True

Method #2: Using Built-in Functions (User Input)

Approach:

  • Give the string as user input using the input() function and store it in a variable.
  • Apply istitle() function to the given string in which if the string is titlecased, the istitle() function returns True. If it does not, it returns False.
  • Store it in another variable.
  • Print the above-given String.
  • Print the result after checking if the given string is titlecased or not.
  • Similarly, do the same for other strings and print the result.
  • The Exit of Program.

Below is the implementation:

# Give the string as user input using the input() function and store it in a variable.
gvn_strng1 = input("Enter some Random String = ")
# Apply istitle() function to the given string in which if the string
# is titlecased, the istitle() function returns True. If it does not,
# it returns False.
# Store it in another variable.
rslt_1 = gvn_strng1.istitle()
# Print the above-given String.
print("The given string = ", gvn_strng1)
# Print the result after checking if the given string is titlecased or not
print("Checking if the given string is titlecased or not = ", rslt_1)
# Similarly, do the same for other strings and print the result.
gvn_strng2 = input("Enter some Random String = ")
rslt_2 = gvn_strng2.istitle()
print("The given string = ", gvn_strng2)
print("Checking if the given string is titlecased or not = ", rslt_2)

gvn_strng3 = input("Enter some Random String = ")
rslt_3 = gvn_strng3.istitle()
print("The given string = ", gvn_strng3)
print("Checking if the given string is titlecased or not = ", rslt_3)

Output:

Enter some Random String = Welcome To Python-programs
The given string = Welcome To Python-programs
Checking if the given string is titlecased or not = False
Enter some Random String = 12345 Hello All
The given string = 12345 Hello All
Checking if the given string is titlecased or not = True
Enter some Random String = GOOD MORNING BTECHGEEKS
The given string = GOOD MORNING BTECHGEEKS
Checking if the given string is titlecased or not = False

Are you facing difficulties in finding all the methods that a string object can call in python? Have a glance at this Python String Method Examples Tutorial & meet such challenges with ease.

Python String istitle() Method with Examples Read More »