Python

Python String index() Method with Examples

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

The index() method looks for the first instance of the specified value.

If the value is not found, the index() method throws an exception.

Note: The index() method is nearly identical to the find() method, with the exception that the find() method returns -1 if the value is not found.

Syntax:

string.index(value, start, end)

Parameters

value: This is Required. It is a string. The value to look for in the string.

start: This is optional. It is an integer. The starting point for the search. The default value is 0.

end: This is optional. It is an integer. The ending point for the search. The default value is the end of the string.

Return Value:

  • If the value is found within the string, it returns the index of the value’s first occurrence.
  • If the value does not exist within the string, a ValueError exception is thrown.

Examples:

Example1:

Input:

Given string = "hello this is btechgeeks this"
Given value = "this "

Output:

The index of the given value's{ this } first occurrence =  6

Example2:

Input:

Given string = "welcome to python programs to all"
Given value = "to"
start position = 20
end position = 30

Output:

The index of the given value's{ to } first occurrence in the given start and end positions :
27

String index() Method with Examples in Python

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

1)Without giving start and end positions

Approach:

  • Give the string as static input and store it in a variable.
  • Give the value as static input and store it in another variable.
  • Pass the given value as an argument to the index() function for the given string which returns the index of the given value’s first occurrence if the value exists and If the value does not exist within the string, a ValueError exception is thrown.
  • Store it in another variable.
  • Print the index of the given value’s first occurrence in the given string.
  • The Exit of the program.

Below is the implementation:

# Give the string as static input and store it in a variable.
gvn_str = "hello this is btechgeeks this"
# Give the value as static input and store it in another variable.
gvn_valu = "this"
# Pass the given value as an argument to the index() function for the given
# string which returns the index of the given value's first occurrence if the
# value exists and If the value does not exist within the string,
# a ValueError exception is thrown.
# Store it in another variable.
rslt = gvn_str.index(gvn_valu)
# Print the index of the given value's first occurrence in the given string.
print(
    "The index of the given value's{", gvn_valu, "} first occurrence = ", rslt)

Output:

The index of the given value's{ this } first occurrence =  6
2)With giving start and end positions

Approach:

  • Give the string as static input and store it in a variable.
  • Give the value as static input and store it in another variable.
  • Give the start position as static input and store it in another variable.
  • Give the end position as static input and store it in another variable.
  • Pass the given value, start and end positions as arguments to the index() function for the given string which returns the index of the given value’s first occurrence, and If the value does not exist within the string, a ValueError exception is thrown for the given start and end range.
  • Store it in another variable.
  • Print the index of the given value’s first occurrence in the given string in the given start and end positions.
  • The Exit of the program.

Below is the implementation:

# Give the string as static input and store it in a variable.
gvn_str = "welcome to python programs to all"
# Give the value as static input and store it in another variable.
gvn_valu = "to"
# Give the start position as static input and store it in another variable.
gvn_strtpositn = 20
# Give the end position as static input and store it in another variable.
gvn_endpositn = 30
# Pass the given value, start and end positions as arguments to the index()
# function for the given string which returns the index of the given value's
# first occurrence, and If the value does not exist within the string,
# a ValueError exception is thrown for the given start and end range.
# Store it in another variable.
rslt = gvn_str.index(gvn_valu, gvn_strtpositn, gvn_endpositn)
# Print the index of the given value's first occurrence in the given string.
print(
    "The index of the given value's{", gvn_valu, "} first occurrence in the given start and end positions :")
print(rslt)

Output:

The index of the given value's{ to } first occurrence in the given start and end positions :
27

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

1)Without giving start and end positions

Approach:

  • Give the string as user input using the input() function and store it in a variable.
  • Give the value as user input using the input() function and store it in another variable.
  • Pass the given value as an argument to the index() function for the given string which returns the index of the given value’s first occurrence if the value exists and If the value does not exist within the string, a ValueError exception is thrown.
  • Store it in another variable.
  • Print the index of the given value’s first occurrence in the given string.
  • The Exit of the program.

Below is the implementation:

# Give the string as user input using the input() function and store it in a variable.
gvn_str = input("Enter some Random String = ")
# Give the value as user input using the input() function and store it in another variable.
gvn_valu = input("Enter some Random String(value) = ")
# Pass the given value as an argument to the index() function for the given
# string which returns the index of the given value's first occurrence if the
# value exists and If the value does not exist within the string,
# a ValueError exception is thrown.
# Store it in another variable.
rslt = gvn_str.index(gvn_valu)
# Print the index of the given value's first occurrence in the given string.
print(
    "The index of the given value's{", gvn_valu, "} first occurrence = ", rslt)

Output:

Enter some Random String = good morning btechgeeks
Enter some Random String(value) = e
The index of the given value's{ e } first occurrence = 15
2)With giving start and end positions

Approach:

  • Give the string as user input using the input() function and store it in a variable.
  • Give the value as user input using the input() function and store it in another variable.
  • Give the start position as user input using the int(input()) function and store it in another variable.
  • Give the end position as user input using the int(input()) function and store it in another variable.
  • Pass the given value, start and end positions as arguments to the index() function for the given string which returns the index of the given value’s first occurrence, and If the value does not exist within the string, a ValueError exception is thrown for the given start and end range.
  • Store it in another variable.
  • Print the index of the given value’s first occurrence in the given string in the given start and end positions.
  • The Exit of the program.

Below is the implementation:

# Give the string as user input using the input() function and store it in a variable.
gvn_str = input("Enter some Random String = ")
# Give the value as user input using the input() function and store it in another variable.
gvn_valu = input("Enter some Random String(value) = ")
# Give the start position as user input using the int(input()) function and 
# store it in another variable.
gvn_strtpositn = int(input("Enter some random number = "))
# Give the end position as user input using the int(input()) function and 
# store it in another variable.
gvn_endpositn = int(input("Enter some random number = "))
# Pass the given value, start and end positions as arguments to the index()
# function for the given string which returns the index of the given value's
# first occurrence, and If the value does not exist within the string,
# a ValueError exception is thrown for the given start and end range.
# Store it in another variable.
rslt = gvn_str.index(gvn_valu, gvn_strtpositn, gvn_endpositn)
# Print the index of the given value's first occurrence in the given string.
print(
    "The index of the given value's{", gvn_valu, "} first occurrence in the given start and end positions :")
print(rslt)

Output:

Enter some Random String = good morning all
Enter some Random String(value) = o
Enter some random number = 3
Enter some random number = 10
The index of the given value's{ o } first occurrence in the given start and end positions :
6

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 index() Method with Examples Read More »

Python String expandtabs() Method with Examples

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

The expandtabs() method expands(sets) the tabsize to the number of whitespaces specified.

The expandtabs() method returns a string copy with all tab characters ‘\t’ replaced with whitespace characters until the next multiple of the tabsize parameter is reached.

Syntax:

string.expandtabs(tabsize)

Parameters

tabsize: This is Optional. It is a number that specifies the tabsize. The default tabsize is eight(8).

Return Value:

expandtabs() returns a string in which all ‘\t’ characters are replaced with whitespace until the next multiple of the tabsize parameter is reached.

Examples:

Example1:

Input:

Given String = "hello\t this is\t btechgeeks\t"

Output:

The string after setting to the default tabsize(8):
hello    this is         btechgeeks

Example2:

Input:

Given String = "good\t morning\t btechgeeks\t"
Given tabsize = 5

Output:

The string after setting to the given tabsize{ 5 } is :
good  morning   btechgeeks

String expandtabs() Method with Examples in Python

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

1)Without Giving Tabsize

Approach:

  • Give the string as static input and store it in a variable.
  • Apply expandtabs() method to the given string that expands(sets) the tabsize to the number of whitespaces specified. The default tabsize is eight(8).
  • Store it in another variable.
  • Print the string after setting to the default tabsize(8).
  • The Exit of the program.

Below is the implementation:

# Give the string as static input and store it in a variable.
gvn_str = "hello\t this is\t btechgeeks\t"
# Apply expandtabs() method to the given string that expands(sets) the tabsize
# to the number of whitespaces specified. The default tabsize is eight(8).
# Store it in another variable.
rslt = gvn_str.expandtabs()
# Print the string after setting to the default tabsize(8).
print("The string after setting to the default tabsize(8):")
print(rslt)

Output:

The string after setting to the default tabsize(8):
hello    this is         btechgeeks
2)With Giving Tabsize

Approach:

  • Give the string as static input and store it in a variable.
  • Give the tabsize as static input and store it in another variable.
  • Apply expandtabs() method to the given string by passing the given tabsize as an argument that expands(sets) the tabsize to the given number of whitespaces specified. The default tabsize is eight(8).
  • Store it in another variable.
  • Print the string after setting it to the given number of whitespaces specified(tabsize).
  • The Exit of the program.

Below is the implementation:

# Give the string as static input and store it in a variable.
gvn_str = "good\t morning\t btechgeeks\t"
# Give the tabsize as static input and store it in another variable.
gvntab_size = 5
# Apply expandtabs() method to the given string by passing the given tabsize
# as an argument that expands(sets) the tabsize to the given number of
# whitespaces specified. The default tabsize is eight(8).
# Store it in another variable.
rslt = gvn_str.expandtabs(gvntab_size)
# Print the string after setting it to the given number of whitespaces
# specified(tabsize).
print("The string after setting to the given tabsize{", gvntab_size, "} is :")
print(rslt)

Output:

The string after setting to the given tabsize{ 5 } is :
good  morning   btechgeeks

Similarly, check it out for the other tabsize.

# Similarly, check it out for the other tabsize.
gvn_str = "good\t morning\t btechgeeks\t"
gvntab_size = 10
rslt = gvn_str.expandtabs(gvntab_size)
print("The string after setting to the given tabsize{", gvntab_size, "} is :")
print(rslt)

Output:

The string after setting to the given tabsize{ 10 } is :
good       morning   btechgeeks
# Similarly, check it out for the other tabsize.
gvn_str = "good\t morning\t btechgeeks\t"
gvntab_size = 1
rslt = gvn_str.expandtabs(gvntab_size)
print("The string after setting to the given tabsize{", gvntab_size, "} is :")
print(rslt)

Output:

The string after setting to the given tabsize{ 1 } is :
good  morning  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 expandtabs() Method with Examples Read More »

Python String endswith() Method with Examples

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

If the string ends with the specified value, the endswith() method returns true; otherwise, it returns False.

Syntax:

string.endswith(value, start, end)

Parameters

value: This is Required. The value used to determine whether the string ends with

start: This is optional. It is an integer. The starting point for the search. The default value is 0.

end: This is optional. It is an integer. The ending point for the search. The default value is the end of the string.

Return Value:

The endswith() method gives a boolean value.

  • If a string ends with the specified value, it returns True.
  • If a string does not end with the specified value, it returns False.

Examples:

Example1:

Input:

Given string = "hello this is btechgeeks.good morning btechgeeks"
Given value = "hello"

Output:

Checking if the given string endswith{ hello } or not =  False

Example2:

Input:

Given string = "welcome to python programs"
Given value = "e"
start position = 0
end position = 7

Output:

Checking if the given string endswith{ e } or not =  True

String endswith() Method with Examples in Python

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

1)Without giving start and end positions

Approach:

  • Give the string as static input and store it in a variable.
  • Give the value as static input and store it in another variable.
  • Pass the given value as an argument to the endswith() function for the given string to check if the given string ends with the given value or not.
  • Store it in another variable.
  • Print the result after checking If a given string ends with the given value or not.
  • The Exit of the program.

Below is the implementation:

# Give the string as static input and store it in a variable.
gvn_str = "hello this is btechgeeks.good morning btechgeeks"
# Give the value as static input and store it in another variable.
gvn_valu = "geeks"
# Pass the given value as an argument to the endswith() function for the
# given string to check if the given string ends with the given value or not.
# Store it in another variable.
rslt = gvn_str.endswith(gvn_valu)
# Print the result after checking If a given string ends with the given
# value or not.
print(
    "Checking if the given string endswith{", gvn_valu, "} or not = ", rslt)

Output:

Checking if the given string endswith{ geeks } or not =  True
2)With giving start and end positions

Approach:

  • Give the string as static input and store it in a variable.
  • Give the value as static input and store it in another variable.
  • Give the start position as static input and store it in another variable.
  • Give the end position as static input and store it in another variable.
  • Pass the given value, start and end positions as arguments to the endswith() function for the given string to check if the given string ends with the given value or not in the given start and end range.
  • Store it in another variable.
  • Print the result after checking If a given string ends with the given value or not.
  • The Exit of the program.

Below is the implementation:

# Give the string as static input and store it in a variable.
gvn_str = "welcome to python programs"
# Give the value as static input and store it in another variable.
gvn_valu = "e"
# Give the start position as static input and store it in another variable.
gvn_strtpositn = 0
# Give the end position as static input and store it in another variable.
gvn_endpositn = 7
# Pass the given value, start and end positions as arguments to the endswith()
# function for the given string to check if the given string ends with the
# given value or not in the given start and end range.
# Store it in another variable.
rslt = gvn_str.endswith(gvn_valu, gvn_strtpositn, gvn_endpositn)
# Print the result after checking If a given string ends with the given
# value or not.
print(
    "Checking if the given string endswith{", gvn_valu, "} or not = ", rslt)

Output:

Checking if the given string endswith{ e } or not =  True

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

1)Without giving start and end positions

Approach:

  • Give the string as user input using the input() function and store it in a variable.
  • Give the value as user input using the input() function and store it in another variable.
  • Pass the given value as an argument to the endswith() function for the given string to check if the given string ends with the given value or not.
  • Store it in another variable.
  • Print the result after checking If a given string ends with the given value or not.
  • The Exit of the program.

Below is the implementation:

# Give the string as user input using the input() function and store it in a variable.
gvn_str = input("Enter some Random String = ")
# Give the value as user input using the input() function and store it in another variable.
gvn_valu = input("Enter some Random String(value) = ")
# Pass the given value as an argument to the endswith() function for the
# given string to check if the given string ends with the given value or not.
# Store it in another variable.
rslt = gvn_str.endswith(gvn_valu)
# Print the result after checking If a given string ends with the given
# value or not.
print(
    "Checking if the given string endswith{", gvn_valu, "} or not = ", rslt)

Output:

Enter some Random String = hello this is btechgeeks
Enter some Random String(value) = hello
Checking if the given string endswith{ hello } or not = False
2)With giving start and end positions

Approach:

  • Give the string as user input using the input() function and store it in a variable.
  • Give the value as user input using the input() function and store it in another variable.
  • Give the start position as user input using the int(input()) function and store it in another variable.
  • Give the end position as user input using the int(input()) function and store it in another variable.
  • Pass the given value, start and end positions as arguments to the endswith() function for the given string to check if the given string ends with the given value or not in the given start and end range.
  • Store it in another variable.
  • Print the result after checking If a given string ends with the given value or not.
  • The Exit of the program.

Below is the implementation:

# Give the string as user input using the input() function and store it in a variable.
gvn_str = input("Enter some Random String = ")
# Give the value as user input using the input() function and store it in another variable.
gvn_valu = input("Enter some Random String(value) = ")
# Give the start position as user input using the int(input()) function and 
# store it in another variable.
gvn_strtpositn = int(input("Enter some random number = "))
# Give the end position as user input using the int(input()) function and 
# store it in another variable.
gvn_endpositn = int(input("Enter some random number = "))
# Pass the given value, start and end positions as arguments to the endswith()
# function for the given string to check if the given string ends with the
# given value or not in the given start and end range.
# Store it in another variable.
rslt = gvn_str.endswith(gvn_valu, gvn_strtpositn, gvn_endpositn)
# Print the result after checking If a given string ends with the given
# value or not.
print(
    "Checking if the given string endswith{", gvn_valu, "} or not = ", rslt)



Output:

Enter some Random String = welcome to python programs
Enter some Random String(value) = p
Enter some random number = 2
Enter some random number = 6
Checking if the given string endswith{ p } 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 endswith() Method with Examples Read More »

Python String count() Method with Examples

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

The count() method returns the number of occurrences of a specified value in the string.

Syntax:

string.count(value, start, end)

Parameters

value: This is Required. It is a string The value to look for in the string.

start: This is optional. It is an integer. The starting point for the search. The default value is 0.

end: This is optional. It is an integer. The ending point for the search. The default value is the end of the string.

Return Value:

The count() method returns the number of times the substring(value) appears in the given string.

Examples:

Example1:

Input:

Given string = "hello this is btechgeeks.good morning btechgeeks"
Given value = "btechgeeks"

Output:

The no of occurences of { btechgeeks } in a given string =  2

Example2:

Input:

Given string = "good morning btechgeeks good"
Given value = "o"
start position = 1
end position = 13

Output:

The no of occurences of { o } in a given string =  3

String count() Method with Examples in Python

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

1)Without giving start and end positions

Approach:

  • Give the string as static input and store it in a variable.
  • Give the value as static input and store it in another variable.
  • Pass the given value as an argument to the count() function for the given string to count the number of times the given value occurred in a given string.
  • Store it in another variable.
  • Print the count of the number of times the given value occurred in a given string.
  • The Exit of the program.

Below is the implementation:

# Give the string as static input and store it in a variable.
gvn_str = "hello this is btechgeeks.good morning btechgeeks"
# Give the value as static input and store it in another variable.
gvn_valu = "btechgeeks"
# Pass the given value as an argument to the count() function for the given
# string to count the number of times the given value occurred in a given string.
# Store it in another variable.
rslt_cnt = gvn_str.count(gvn_valu)
# Print the count of the number of times the given value occurred in
# a given string.
print("The no of occurences of {", gvn_valu, "} in a given string = ", rslt_cnt)

Output:

The no of occurences of { btechgeeks } in a given string =  2
2)With giving start and end positions

Approach:

  • Give the string as static input and store it in a variable.
  • Give the value as static input and store it in another variable.
  • Give the start position as static input and store it in another variable.
  • Give the end position as static input and store it in another variable.
  • Pass the given value, start and end positions as arguments to the count() function for the given string to count the number of times the given value occurred in a given string in the given start and end range.
  • Store it in another variable.
  • Print the count of the number of times the given value occurred in a given string.
  • The Exit of the program.

Below is the implementation:

# Give the string as static input and store it in a variable.
gvn_str = "hello all hello good morning hello"
# Give the value as static input and store it in another variable.
gvn_valu = "hello"
# Give the start position as static input and store it in another variable.
gvn_strtpositn = 6
# Give the end position as static input and store it in another variable.
gvn_endpositn = 16
# Pass the given value, start, end positions as arguments to the count() function
# for the given string to count the number of times the given value occurred in a
# given string in the given start and end range.
# Store it in another variable.
rslt_cnt = gvn_str.count(gvn_valu, gvn_strtpositn, gvn_endpositn)
# Print the count of the number of times the given value occurred in
# a given string.
print("The no of occurences of {", gvn_valu,
      "} in a given string = ", rslt_cnt)

Output:

The no of occurences of { hello } in a given string =  1

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

1)Without giving start and end positions

Approach:

  • Give the string as user input using the input() function and store it in a variable.
  • Give the value as user input using the input() function and store it in another variable.
  • Pass the given value as an argument to the count() function for the given string to count the number of times the given value occurred in a given string.
  • Store it in another variable.
  • Print the count of the number of times the given value occurred in a given string.
  • The Exit of the program.

Below is the implementation:

# Give the string as user input using the input() function and store it in a variable.
gvn_str = input("Enter some Random String = ")
# Give the value as user input using the input() function and store it in another variable.
gvn_valu = input("Enter some Random String(value) = ")
# Pass the given value as an argument to the count() function for the given
# string to count the number of times the given value occurred in a given string.
# Store it in another variable.
rslt_cnt = gvn_str.count(gvn_valu)
# Print the count of the number of times the given value occurred in
# a given string.
print("The no of occurences of {", gvn_valu, "} in a given string = ", rslt_cnt)

Output:

Enter some Random String = welcome to python programs to to all
Enter some Random String(value) = to
The no of occurences of { to } in a given string = 3
2)With giving start and end positions

Approach:

  • Give the string as user input using the input() function and store it in a variable.
  • Give the value as user input using the input() function and store it in another variable.
  • Give the start position as user input using the int(input()) function and store it in another variable.
  • Give the end position as user input using the int(input()) function and store it in another variable.
  • Pass the given value, start and end positions as arguments to the count() function for the given string to count the number of times the given value occurred in a given string in the given start and end range.
  • Store it in another variable.
  • Print the count of the number of times the given value occurred in a given string.
  • The Exit of the program.

Below is the implementation:

# Give the string as user input using the input() function and store it in a variable.
gvn_str = input("Enter some Random String = ")
# Give the value as user input using the input() function and store it in another variable.
gvn_valu = input("Enter some Random String(value) = ")
# Give the start position as user input using the int(input()) function and 
# store it in another variable.
gvn_strtpositn = int(input("Enter some random number = "))
# Give the end position as user input using the int(input()) function and 
# store it in another variable.
gvn_endpositn = int(input("Enter some random number = "))
# Pass the given value, start, end positions as arguments to the count() function
# for the given string to count the number of times the given value occurred in a
# given string in the given start and end range.
# Store it in another variable.
rslt_cnt = gvn_str.count(gvn_valu, gvn_strtpositn, gvn_endpositn)
# Print the count of the number of times the given value occurred in
# a given string.
print("The no of occurences of {", gvn_valu,
      "} in a given string = ", rslt_cnt)





Output:

Enter some Random String = good morning btechgeeks good 
Enter some Random String(value) = o
Enter some random number = 1
Enter some random number = 13
The no of occurences of { o } in a given string = 3

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 count() Method with Examples Read More »

Python String translate() Method with Examples

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

The translate() method returns a string in which certain characters are replaced with the character described in a dictionary or mapping table.

To create a mapping table, use the maketrans() method.

If a character is not found in the dictionary/table, it will not be replaced.

When using a dictionary, you must use ascii codes rather than characters.

Syntax:

string.translate(table)

Parameter

table: This is Required. Either a dictionary or a mapping table describing how to replace.

Return value:

The translate() method returns a string with each character mapped to its corresponding character according to the translation table.

Examples:

Example1:

Input:

Given first String = "pqr"
Given second String = "stu"
Given third String = "ab"
Given string = "pqrs"

Output:

The given string: pqrs
The translated string for the given string is: stus

Example2:

Input:

Given first String = "efg"
Given second String = "hij"
Given third String = "fg"
Given string = "efghij"

Output:

The given string: efghij
The translated string for the given string is: hhij

String translate() Method 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 second string as static input and store it in another variable.
  • Give the third string as static input and store it in another variable.
  • Give some random string as static input and store it in another variable.
  • Print the above-given string.
  • Pass the given first, second and third strings as arguments to the maketrans() function for the given string to create a mapping table.
  • Store it in another variable.
  • Translate the above result for the given string using the translate() function and print it.
  • The Exit of the program.

Below is the implementation:

# Give the first string as static input and store it in a variable.
gvn_fststr = "efg"
# Give the second string as static input and store it in another variable.
gvn_scndstr = "hij"
# Give the third string as static input and store it in another variable.
gvn_thrdstr = "fg"
# Give some random string as static input and store it in another variable.
gvn_str = "efghij"
# Print the above-given string.
print("The given string:", gvn_str)
# Pass the given first, second and third strings as arguments to the maketrans()
# function for the given string to create a mapping table.
# Store it in another variable.
translatn = gvn_str.maketrans(gvn_fststr, gvn_scndstr, gvn_thrdstr)
# Translate the above result for the given string using the translate()
# function and print it.
print("The translated string for the given string is:",
      gvn_str.translate(translatn))

Output:

The given string: efghij
The translated string for the given string is: hhij

Explanation:

The mapping translation here contains the mapping from e f, and g to h, i, and j respectively.

The removal string third string, on the other hand, resets the mapping to e and f to None.

As a result, when the string is translated with translate(), e and f are removed and g is replaced with idef.

For Further More Explanation on translation Please look at the below article :

Python String maketrans() Method with Examples

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 second string as user input using the input() function and store it in another variable.
  • Give the third string as user input using the input() function and store it in another variable.
  • Give some random string as user input using the input() function and store it in another variable.
  • Print the above-given string.
  • Pass the given first, second and third strings as arguments to the maketrans() function for the given string to create a mapping table.
  • Store it in another variable.
  • Translate the above result for the given string using the translate() function and print it.
  • The Exit of the 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 second string as user input using the input() function and store it in another variable.
gvn_scndstr = input("Enter some Random String = ")
# Give the third string as user input using the input() function and store it in another variable.
gvn_thrdstr = input("Enter some Random String = ")
# Give some random string as static input and store it in another variable.
gvn_str = input("Enter some Random String = ")
# Print the above-given string.
print("The given string:", gvn_str)
# Pass the given first, second and third strings as arguments to the maketrans()
# function for the given string to create a mapping table.
# Store it in another variable.
translatn = gvn_str.maketrans(gvn_fststr, gvn_scndstr, gvn_thrdstr)
# Translate the above result for the given string using the translate()
# function and print it.
print("The translated string for the given string is:",
      gvn_str.translate(translatn))

Output:

Enter some Random String = pqr
Enter some Random String = stu
Enter some Random String = ab
Enter some Random String = pqrs
The given string: pqrs
The translated string for the given string is: stus

Translation with translate() with manual translation table(for dictionaries)

Below is the implementation:

gvn_dict = {101: 98, 104: 97, 102: None}
gvn_str = "efghij"
print("The given string is:", gvn_str)
print("The translated string for the given string is:",
      gvn_str.translate(gvn_dict))

Output:

The given string is: efghij
The translated string for the given string is: bgaij

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 translate() Method with Examples Read More »

Python List extend() Method with Examples

In the previous article, we have discussed Python List count() 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 extend() Method in Python:

The extend() method appends all iterable (list, tuple, string, etc.) items to the end of the list.

Syntax:

gvnlist.extend(iterable)

All iterable elements are appended to the end of gvnlist in this case.

Parameter Values: 

As previously stated, the extend() method accepts an iterable such as a list, tuple, string, and so on.

Return Value:

The extend() method makes changes to the original list. It doesn’t return anything.

Examples:

Example1:

Input:

Given Main list = ['hello', 'this']
Given Second List = ['python', 'programs']

Output:

The Modified list after extending is :  ['hello', 'this', 'python', 'programs']

Example2:

Input:

Given Main list = ['hello', 'this']
Given Second Tuple= ('python', 'programs', 'python')

Output:

The Modified list after extending is :  ['hello', 'this', 'python', 'programs', 'python']

List extend() Method with Examples in Python

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

Example-1: Adding Another List

Approach:

  • Give the main list as static input and store it in a variable.
  • Give the list which we want to extend as static input and store it in another variable.
  • Extend the second list to the main list using the extend() function(Here it appends the second list to main list).
  • Print the elements of the main list.
  • The Exit of the Program.

Below is the implementation:

# Give the main list as static input and store it in a variable.
mainlst = ['hello', 'this']
# Give the list which we want to extend as static input and store it in another variable.
scndlist = ['python', 'programs']
# Extend the second list to the main list using the extend()
# function(Here it appends the second list to main list)

mainlst.extend(scndlist)
# Print the mainlist
print('The Modified list after extending is : ', mainlst)

Output:

The Modified list after extending is :  ['hello', 'this', 'python', 'programs']
Example-2: Adding Another Set

Approach:

  • Give the main list as static input and store it in a variable.
  • Give the set which we want to extend as static input and store it in another variable.
  • Extend the second set to the main list using the extend() function(Here it appends the second set to main list) and store it in a variable resltlst.
  • Print the elements of the main list.
  • The Exit of the Program.

Below is the implementation:

# Give the main list as static input and store it in a variable.
mainlst = ['hello', 'this']
# Give the set which we want to extend as static input and store it in another variable.
scndset = {'python', 'programs', 'python'}
# Extend the second set to the main list using the extend()
# function(Here it appends the second set to main list)

mainlst.extend(scndset)
# Print the mainlist
print('The Modified list after extending is : ', mainlst)

Output:

The Modified list after extending is :  ['hello', 'this', 'programs', 'python']
Example-3: Adding Another Tuple

Approach:

  • Give the main list as static input and store it in a variable.
  • Give the tuple which we want to extend as static input and store it in another variable.
  • Extend the second tuple to the main list using the extend() function(Here it appends the second tuple to the main list).
  • Print the elements of the main list.
  • The Exit of the Program.

Below is the implementation:

# Give the main list as static input and store it in a variable.
mainlst = ['hello', 'this']
# Give the tuple which we want to extend as static input and store it in another variable.
scndtup = ('python', 'programs', 'python')
# Extend the second tuple to the main list using the extend() function
# (Here it appends the second tuple to the main list).

mainlst.extend(scndtup)
# Print the mainlist
print('The Modified list after extending is : ', mainlst)

Output:

The Modified list after extending is :  ['hello', 'this', 'python', 'programs', 'python']

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

Approach:

  • Give the main list as user input using list(),int(),split() and map() functions and store it in a variable.
  • Give the list which we want to extend as user input using list(),int(),split() and map() functions and store it in another variable.
  • Extend the second list to the main list using the extend() function(Here it appends the second list to main list).
  • Print the elements of the main list.
  • The Exit of the Program.

Below is the implementation:

# Give the main list as user input using list(),int(),split() and map() functions and store it in a variable.
mainlst = list(
    map(int, input('Enter some random elements of the main list = ').split()))
# Give the list which we want to extend as user input using list(),int(),split() and map() functions
# and store it in another variable.
scndlst =  list(
    map(int, input('Enter some random elements of the second list = ').split()))
# Extend the second list to the main list using the extend() function
# (Here it appends the second list to the main list).

mainlst.extend(scndlst)
# Print the mainlist
print('The Modified list after extending is : ', mainlst)

Output:

Enter some random elements of the main list = 0 3 0 8
Enter some random elements of the second list = 2 8 3 7 9 4
The Modified list after extending is : [0, 3, 0, 8, 2, 8, 3, 7, 9, 4]

Covered all examples on python concepts and missed checking out the Python List Method Examples then go with the available link & gain more knowledge about List method concepts in python.

Python List extend() Method with Examples Read More »

Python List count() method with Examples

In the previous article, we have discussed Python Tuple index() 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 count() method in Python:

The count() function returns the number of occurrences of the particular element in the given list.

Syntax:

givenlist.count(gvnelement)

Parameters:

The count() method only accepts one argument.

  • gvnelement: The element whose frequency is to be calculated

Return Value:

The count() method returns the number of occurrences of the given element in the given List.

Examples:

Example1:

Input:

Given List = ['hello', 'this', 'is', 'python', 'is', 'programs', 'python']
Given Element =  "python"

Output:

The count of the given element { python } in the given List ['hello', 'this', 'is', 'python', 'is', 'programs', 'python'] is :
2

Example2:

Input:

Given List =  [7, 1, 8, 2, 8, 1, 9, 8, 4]
Given Element = 8

Output:

Enter some random list elements = 7 1 8 2 8 1 9 8 4
Enter some random element = 8
The count of the given element { 8 } in the given list [7, 1, 8, 2, 8, 1, 9, 8, 4] is :
3

List count() method with Examples in Python

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

Approach:

Example-1:

String List:

  • Give the string List as static input and store it in a variable.
  • Give the element whose frequency is to be calculated as static input and store it in another variable.
  • Pass the given element above as the argument to the count function for the given List and store the result in a variable say resltcnt(It has the count of the given element in the given List).
  • Print the value of resltcnt.
  • The Exit of the Program.

Below is the implementation:

# Give the string List as static input and store it in a variable.
gvnlst = ['hello', 'this', 'is', 'python', 'is', 'programs', 'python']
# Give the element whose frequency is to be calculated as static input
# and store it in another variable.
gvnelemeent = "python"
# Pass the given element above as the argument to the count function
# for the given List and store the result in a variable say resltcnt
# (It has the count of the given element in the given List).
resltcnt = gvnlst.count(gvnelemeent)
# Print the value of resltcnt.
print('The count of the given element {', gvnelemeent,
      '} in the given List', gvnlst, 'is :')
print(resltcnt)

Output:

The count of the given element { python } in the given List ['hello', 'this', 'is', 'python', 'is', 'programs', 'python'] is :
2

Example-2:

Integer List:

  • Give the Integer List as static input and store it in a variable.
  • Give the element whose frequency is to be calculated as static input and store it in another variable.
  • Pass the given element above as the argument to the count function for the given List and store the result in a variable say resltcnt(It has the count of the given element in the given List ).
  • Print the value of resltcnt.
  • The Exit of the Program.

Below is the implementation:

# Give the Integer  List as static input and store it in a variable.
gvnlst = [1, 2, 3, 4, 5, 6, 2, 3, 4, 5, 6, 11, 2, 3, 45, 6, 2, 3, 5]
# Give the element whose frequency is to be calculated as static input
# and store it in another variable.
gvnelemeent = 3
# Pass the given element above as the argument to the count function
# for the given List and store the result in a variable say resltcnt
# (It has the count of the given element in the given List).
resltcnt = gvnlst.count(gvnelemeent)
# Print the value of resltcnt.
print('The count of the given element {', gvnelemeent,
      '} in the given List', gvnlst, 'is :')
print(resltcnt)

Output:

The count of the given element { 3 } in the given List [1, 2, 3, 4, 5, 6, 2, 3, 4, 5, 6, 11, 2, 3, 45, 6, 2, 3, 5] is :
4

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

Approach:

  • Give the Integer List as user input using list(),map(),input(),and split() functions.
  • Store it in a variable.
  • Give the element as user input using the int(), input() functions, and store it in another variable.
  • Pass the given element above as the argument to the count function for the given List and store the result in a variable say resltcnt(It has the count of the given element in the given List).
  • Print the value of resltcnt.
  • The Exit of Program.

Below is the implementation:

# Give the Integer List as user input using list(),map(),input(),and split() functions.
gvnlistt = list(map(int,input('Enter some random list elements = ').split()))
# Give the element as user input using the int(), input() functions,
# and store it in another variable.
gvnelemeent = int(input('Enter some random element = '))
# Pass the given element above as the argument to the count function
# for the given list and store the result in a variable say resltcnt
# (It has the count of the given element in the given list).
resltcnt = gvnlistt.count(gvnelemeent)
# Print the value of resltcnt.
print('The count of the given element {', gvnelemeent,
      '} in the given list', gvnlistt, 'is :')
print(resltcnt)

Output:

Enter some random list elements = 7 1 8 2 8 1 9 8 4
Enter some random element = 8
The count of the given element { 8 } in the given list [7, 1, 8, 2, 8, 1, 9, 8, 4] is :
3

Covered all examples on python concepts and missed checking out the Python List Method Examples then go with the available link & gain more knowledge about List method concepts in python.

Python List count() method with Examples Read More »

Python List remove() Method with Examples

In the previous article, we have discussed Python List pop() 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 remove() Method in Python:

The remove() method deletes the element’s first occurrence with the specified value.

Syntax:

list.remove(element)

Parameters

  • The remove() method accepts a single argument and removes it from the list.
  • If the element does not exist, a ValueError: list.remove(x): x not in list exception is thrown.

Return Value:

This function does not return any value.

Examples:

Example1:

Input:

Given List = ['hello', 'this', 'is', 'btechgeeks']
Given element to be removed = 'hello'

Output:

The given original list is: ['hello', 'this', 'is', 'btechgeeks']
The given list after removing the given element{ hello } is :
['this', 'is', 'btechgeeks']

Example2:

Input:

Given List = [-1, 0, 2, 8, 9, 2]
Given element to be removed = 2

Output:

The given original list is: [-1, 0, 2, 8, 9, 2]
The given list after removing the given element{ 2 } is :
[-1, 0, 8, 9, 2]

List remove() 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 original list.
  • Give the element to be removed as static input and store it in another variable.
  • Pass the given element as an argument to the remove() method for the given list that removes the given element from the given list.
  • Print the list after removing the given element.
  • 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 original list.
print("The given original list is:", gvn_lst)
# Give the element to be removed as static input and store it in another variable.
gvn_elemnt = 'hello'
# Pass the given element as an argument to the remove() method for the
# given list that removes the given element from the given list.
gvn_lst.remove(gvn_elemnt)
# Print the list after removing the given element.
print("The given list after removing the given element{", gvn_elemnt, "} is :")
print(gvn_lst)

Output:

The given original list is: ['hello', 'this', 'is', 'btechgeeks']
The given list after removing the given element{ hello } is :
['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 original list.
  • Give the element to be removed as user input using the int(input()) function and store it in another variable.
  • Pass the given element as an argument to the remove() method for the given list that removes the given element from the given list.
  • Print the list after removing the given element.
  • 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 original list.
print("The given original list is:", gvn_lst)
# Give the element to be removed as user input using the int(input()) function 
# and store it in another variable.
gvn_elemnt = int(input("Enter some random number = "))
# Pass the given element as an argument to the remove() method for the
# given list that removes the given element from the given list.
gvn_lst.remove(gvn_elemnt)
# Print the list after removing the given element.
print("The given list after removing the given element{", gvn_elemnt, "} is :")
print(gvn_lst)

Output:

Enter some random List Elements separated by spaces = -1 0 2 8 9
The given original list is: [-1, 0, 2, 8, 9]
Enter some random number = 2
The given list after removing the given element{ 2 } is :
[-1, 0, 8, 9]

Covered all examples on python concepts and missed checking out the Python List Method Examples then go with the available link & gain more knowledge about List method concepts in python.

 

Python List remove() Method with Examples Read More »

Python Tuple index() method with Examples

In the previous article, we have discussed Python Tuple count() method with Examples
Tuples in Python:

Tuples are a data structure in Python that stores an ordered succession of values. They are unchangeable. This signifies that the values of a tuple cannot be changed. They allow you to save an organized list of items. A tuple, for example, can be used to hold a list of employee names.

Tuple index() Method in Python:

The index() method returns the position in the given tuple of the given element.

Syntax:

gvntuple .index(element, start, end)

Parameter Values: 

The tuple index() method can accept up to three arguments:

element – the to-be-searched element

start (optional) – Begin searching from this index

end (optional) – Search the element all the way up to this index

Return Value:

The index() method returns the index of the specified tuple element.
A ValueError exception is thrown if the element is not found.

Note:

Only the first occurrence of the matching element is returned by the index() method.

Examples:

Example1:

Input:

Given Tuple= ('hello', 'this', 'is', 'this', 'python', 'programs')
Given Element = 'this'

Output:

The given element { this } in the given tuple ('hello', 'this', 'is', 'this', 'python', 'programs') is present at the index :
1

Example2:

Input:

Given Tuple= ('hello', 'this', 'is', 'this', 'python', 'programs')
Given Element = 'morning'

Output:

Traceback (most recent call last):
  File "/home/a3ee758dfee37d1702ad5c70e38293aa.py", line 8, in <module>
    resltind = gvntupl.index(gvnele)
ValueError: tuple.index(x): x not in tuple

Tuple index() Method with Examples in Python

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

Example-1: If the element is present in the given Tuple

Approach:

  • Give the Tuple as the static input and store it in a variable.
  • Give the element whose index must be located in the given Tuple and store it in another variable.
  • Pass the given element to the index function for the given Tuple and store the result in resltind variable.
  • Print the resltind value.
  • The Exit of the Program.

Below is the implementation:

# Give the tuple as the static input and store it in a variable.
gvntupl = ('hello', 'this', 'is', 'this', 'python', 'programs')
# Give the element whose index must be located in the given tuple
# and store it in another variable.
gvnele = 'this'
# Pass the given element to the index function for the given tuple
# and store the result in resltind variable.
resltind = gvntupl.index(gvnele)
# Print the resltind value.
print('The given element {', gvnele, '} in the given tuple',
      gvntupl, 'is present at the index :')
print(resltind)

Output:

The given element { this } in the given tuple ('hello', 'this', 'is', 'this', 'python', 'programs') is present at the index :
1
Example-2: If the element is not present in the given Tuple

Below is the implementation:

# Give the tuple as the static input and store it in a variable.
gvntupl = ('hello', 'this', 'is', 'this', 'python', 'programs')
# Give the element whose index must be located in the given tuple
# and store it in another variable.
gvnele = 'now'
# Pass the given element to the index function for the given tuple
# and store the result in resltind variable.
resltind = gvntupl.index(gvnele)
# Print the resltind value.
print('The given element {', gvnele, '} in the given tuple',
      gvntupl, 'is present at the index :')
print(resltind)

Output:

Traceback (most recent call last):
  File "/home/a3ee758dfee37d1702ad5c70e38293aa.py", line 8, in <module>
    resltind = gvntupl.index(gvnele)
ValueError: tuple.index(x): x not in tuple
Example-3: Giving Start Index

Below is the implementation:

# Give the tuple as the static input and store it in a variable.
gvntupl = ('hello', 'this', 'is', 'this', 'python', 'programs')
# Give the element whose index must be located in the given tuple
# and store it in another variable.
gvnele = 'this'
# Pass the given element to the index function for the given tuple
# and store the result in resltind variable.
# searching the element after 1 st index by passing second argument as 1
resltind = gvntupl.index(gvnele, 1)
# Print the resltind value.
print('The given element {', gvnele, '} in the given tuple',
      gvntupl, 'is present at the index :')
print(resltind)

Output:

The given element { this } in the given tuple ('hello', 'this', 'is', 'this', 'python', 'programs') is present at the index :
1
Example-4: Giving Start and End Index

Below is the implementation:

# Give the tuple as the static input and store it in a variable.
gvntupl = ('hello', 'this', 'is', 'this', 'python', 'programs')
# Give the element whose index must be located in the given tuple
# and store it in another variable.
gvnele = 'hello'
# Pass the given element to the index function for the given tuple
# and store the result in resltind variable.
# searching the element after 1 st index to 3 index  by passing
# second argument as 1 and third argument as 3
resltind = gvntupl.index(gvnele, 1, 3)
# Print the resltind value.
print('The given element {', gvnele, '} in the given tuple',
      gvntupl, 'is present at the index :')
print(resltind)

Output:

Traceback (most recent call last):
  File "/home/0eabc1fa82ac939d529fa63ad0fd9be6.py", line 10, in <module>
    resltind = gvntupl.index(gvnele, 1, 3)
ValueError: tuple.index(x): x not in tuple

Explanation:

The element hello is present at the 0th index but not between the indices, resulting in an error.

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

Approach:

  • Give the Tuple as user input using tuple(),map(),input(),and split() functions.
  • Store it in a variable.
  • Give the element whose index must be located in the given Tuple as user input using the input() function and store it in another variable.
  • Pass the given element to the index function for the given Tuple and store the result in resltind variable.
  • Print the resltind value.
  • The Exit of the Program.

Below is the implementation:

# Give the Integer tuple as user input using tuple (),map(),input(),and split() functions.
# Store it in a variable.
gvntupl = tuple(map(int, input('Enter some random tuple elements = ').split()))
# Give the element as user input using the int(), input() functions,
# and store it in another variable.
gvnele = int(input('Enter some random element = '))
# Pass the given element to the index function for the given tuple
# and store the result in resltind variable.
resltind = gvntupl.index(gvnele)
# Print the resltind value.
print('The given element {', gvnele, '} in the given tuple',
      gvntupl, 'is present at the index :')
print(resltind)

Output:

Enter some random tuple elements = 4 1 7 8 3 11 7 3
Enter some random element = 3
The given element { 3 } in the given tuple (4, 1, 7, 8, 3, 11, 7, 3) is present at the index :
4

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 Tuple index() method with Examples Read More »

Python Tuple count() method with Examples

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

Tuples are a data structure in Python that stores an ordered succession of values. They are unchangeable. This signifies that the values of a tuple cannot be changed. They allow you to save an organized list of items. A tuple, for example, can be used to hold a list of employee names.

Tuple count() method in Python:

The count() function returns the number of occurrences of the particular element in the given tuple.

Syntax:

giventuple.count(gvnelement)

Parameters:

The count() method only accepts one argument.

  • gvnelement: The element whose frequency is to be calculated

Return Value:

The count() method returns the number of occurrences of the given element in the given tuple.

Examples:

Example1:

Input:

Given Tuple = ('hello', 'this', 'is', 'python', 'is', 'programs')
Given Element = "is"

Output:

The count of the given element { is } in the given tuple ('hello', 'this', 'is', 'python', 'is', 'programs') is :
2

Example2:

Input:

Given tuple = (11, 44, 13, 11, 73, 11, 28, 282, 28, 19, 28, 1, 28)
Given Element = 28

Output:

The count of the given element { 28 } in the given tuple (11, 44, 13, 11, 73, 11, 28, 282, 28, 19, 28, 1, 28) is :
4

Tuple count() method with Examples in Python

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

Approach:

Example-1:

String Tuple:

  • Give the string tuple as static input and store it in a variable.
  • Give the element whose frequency is to be calculated as static input and store it in another variable.
  • Pass the given element above as the argument to the count function for the given tuple and store the result in a variable say resltcnt(It has the count of the given element in the given tuple).
  • Print the value of resltcnt.
  • The Exit of the Program.

Below is the implementation:

# Give the string tuple as static input and store it in a variable.
gvntupl = ('hello', 'this', 'is', 'python', 'is', 'programs')
# Give the element whose frequency is to be calculated as static input
# and store it in another variable.
gvnelemeent = "is"
# Pass the given element above as the argument to the count function
# for the given tuple and store the result in a variable say resltcnt
# (It has the count of the given element in the given tuple).
resltcnt = gvntupl.count(gvnelemeent)
# Print the value of resltcnt.
print('The count of the given element {', gvnelemeent,
      '} in the given tuple', gvntupl, 'is :')
print(resltcnt)

Output:

The count of the given element { is } in the given tuple ('hello', 'this', 'is', 'python', 'is', 'programs') is :
2

Example-2:

Integer Tuple:

  • Give the Integer tuple as static input and store it in a variable.
  • Give the element whose frequency is to be calculated as static input and store it in another variable.
  • Pass the given element above as the argument to the count function for the given tuple and store the result in a variable say resltcnt(It has the count of the given element in the given tuple).
  • Print the value of resltcnt.
  • The Exit of the Program.

Below is the implementation:

# Give the Integer tuple as static input and store it in a variable.
gvntupl = (11, 44, 13, 11, 73, 11, 28, 282, 28, 19, 28, 1, 28)
# Give the element whose frequency is to be calculated as static input
# and store it in another variable.
gvnelemeent = 28
# Pass the given element above as the argument to the count function
# for the given tuple and store the result in a variable say resltcnt
# (It has the count of the given element in the given tuple).
resltcnt = gvntupl.count(gvnelemeent)
# Print the value of resltcnt.
print('The count of the given element {', gvnelemeent,
      '} in the given tuple', gvntupl, 'is :')
print(resltcnt)

Output:

The count of the given element { 28 } in the given tuple (11, 44, 13, 11, 73, 11, 28, 282, 28, 19, 28, 1, 28) is :
4

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

Approach:

  • Give the Integer tuple as user input using tuple (),map(),input(),and split() functions.
  • Store it in a variable.
  • Give the element as user input using the int(), input() functions, and store it in another variable.
  • Pass the given element above as the argument to the count function for the given tuple and store the result in a variable say resltcnt(It has the count of the given element in the given tuple).
  • Print the value of resltcnt.
  • The Exit of Program.

Below is the implementation:

# Give the Integer tuple as user input using tuple (),map(),input(),and split() functions.
# Store it in a variable.
gvntupl = tuple(map(int, input('Enter some random tuple elements = ').split()))
# Give the element as user input using the int(), input() functions,
# and store it in another variable.
gvnelemeent = int(input('Enter some random element = '))
# Pass the given element above as the argument to the count function
# for the given tuple and store the result in a variable say resltcnt
# (It has the count of the given element in the given tuple).
resltcnt = gvntupl.count(gvnelemeent)
# Print the value of resltcnt.
print('The count of the given element {', gvnelemeent,
      '} in the given tuple', gvntupl, 'is :')
print(resltcnt)

Output:

Enter some random tuple elements = 12 7 1 8 3 2 1 7 1 1
Enter some random element = 1
The count of the given element { 1 } in the given tuple (12, 7, 1, 8, 3, 2, 1, 7, 1, 1) is :
4

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 Tuple count() method with Examples Read More »