Author name: Vikram Chiluka

Python String upper() Method with Examples

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

The upper() method returns a string in which all characters are capitalized(upper case).

Symbols and numbers are not accepted.

Syntax: 

string.upper()

Parameter Values: This method has no parameters.

Return Value:

upper() returns the uppercase string from a given string.

All lowercase characters are converted to uppercase.

If no lowercase characters are found, the original string is returned.

Examples:

Example1:

Input:

Given String = "hello THIS iS btechgeeks"

Output:

The given original string : hello THIS iS btechgeeks
The given string after applying upper() method :
HELLO THIS IS BTECHGEEKS

Example2:

Input:

Given String = "gooD mornING btechgeeks @!@#123"

Output:

The given original string : gooD mornING btechgeeks @!@#123
The given string after applying upper() method :
GOOD MORNING BTECHGEEKS @!@#123

String upper() Method with 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 the upper() method to the given string that returns a string with all characters in the upper case.
  • Store it in another variable.
  • Print the given original string.
  • Print the given string after applying the upper () method.
  • 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"
# Apply upper() method to the given string that returns a string with all
# characters in upper case.
# Store it in another variable.
rslt = gvn_str.upper()
# Print the given original string.
print("The given original string :", gvn_str)
# Print the given string after applying the upper() method.
print("The given string after applying upper() method :")
print(rslt)

Output:

The given original string : hello THIS iS btechgeeks
The given string after applying upper() method :
HELLO THIS IS BTECHGEEKS

How does upper() function work in a program?

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.
  • Converting both the given first and second strings into uppercase using the upper() method and checking if both the strings are the same or not using the if conditional statement.
  • If it is true, then print “The given first and second strings are same”.
  • Else print “The given first and second strings are NOT same”.
  • The Exit of the program.

Below is the implementation:

# Give the first string as static input and store it in a variable.
gvn_fststr = "WELCOME to python PROGRAMS"
# Give the second string as static input and store it in a variable.
gvn_scndstr = "Welcome to PYTHON programs"
# Converting both the first and second strings into uppercase using the upper()
# method and checking if both the strings are same or not using the if conditional
# statement.
if(gvn_fststr.upper() == gvn_scndstr.upper()):
    # If it is true, then print "The given first and second strings are same".
    print("The given first and second strings are same")
else:
    # Else print "The given first and second strings are NOT same".
    print("The given first and second strings are NOT same")

Output:

The given first and second strings are same

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 the upper() method to the given string that returns a string with all characters in the upper case.
  • Store it in another variable.
  • Print the given original string.
  • Print the given string after applying the upper () method.
  • 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 = ")
# Apply upper() method to the given string that returns a string with all
# characters in upper case.
# Store it in another variable.
rslt = gvn_str.upper()
# Print the given original string.
print("The given original string :", gvn_str)
# Print the given string after applying the upper() method.
print("The given string after applying upper() method :")
print(rslt)

Output:

Enter some Random String = gooD mornING btechgeeks @!@#123
The given original string : gooD mornING btechgeeks @!@#123
The given string after applying upper() method :
GOOD MORNING BTECHGEEKS @!@#123

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

Python String find() Method with Examples

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

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

If the value cannot be found, the find() method returns -1.

The find() method is nearly identical to the index() method, with the exception that the index() method throws an exception if the value is not found.

Syntax:

string.find(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 find() method gives the following integer value:

  • If the value exists within the string, it returns the index of the value’s first occurrence.
  • It returns -1 if a value does not exist within the string.

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 find() 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 find() function for the given string which returns the index of the given value’s first occurrence if the value exists and returns -1 if a value does not exist within the given string.
  • 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 find() function for the given
# string which returns the index of the given value's first occurrence if the
# value exists and returns -1 if a value does not exist within the given string.
# Store it in another variable.
rslt = gvn_str.find(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 find() function for the given string which returns the index of the given value’s first occurrence if the value exists and returns -1 if a value does not exist within the given string in 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 find()
# function for the given string which returns the index of the given value's
# first occurrence if the value exists and returns -1 if a value does not
# exist within the given string in the given start and end range.
# Store it in another variable.
rslt = gvn_str.find(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 find() function for the given string which returns the index of the given value’s first occurrence if the value exists and returns -1 if a value does not exist within the given string.
  • 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 find() function for the given
# string which returns the index of the given value's first occurrence if the
# value exists and returns -1 if a value does not exist within the given string.
# Store it in another variable.
rslt = gvn_str.find(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 find() function for the given string which returns the index of the given value’s first occurrence if the value exists and returns -1 if a value does not exist within the given string in 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 find()
# function for the given string which returns the index of the given value's
# first occurrence if the value exists and returns -1 if a value does not
# exist within the given string in the given start and end range.
# Store it in another variable.
rslt = gvn_str.find(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 find() Method with Examples Read More »

Python String lower() Method with Examples

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

lower() returns a string with all characters in lower case.

Symbols and numbers are not accepted.

Syntax: 

string.lower()

Parameter Values: This method has no parameters.

Return Value:

lower() returns the lowercase string from a given string. It converts all uppercase to lowercase characters.

If no uppercase characters are found, the original string is returned.

Examples:

Example1:

Input:

Given String = "HELLO this IS Btechgeeks"

Output:

The given original string : HELLO this IS Btechgeeks
The given string after applying lower() method :
hello this is btechgeeks

Example2:

Input:

Given String = "GooD MORNING btechgeeks@123$$#"

Output:

The given original string : GooD MORNING btechgeeks@123$$#
The given string after applying lower() method :
good morning btechgeeks@123$$#

String lower() Method with 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 the lower() method to the given string that returns a string with all characters in lower case.
  • Store it in another variable.
  • Print the given original string.
  • Print the given string after applying the lower() method.
  • 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"
# Apply lower() method to the given string that returns a string with all characters
# in lower case.
# Store it in another variable.
rslt = gvn_str.lower()
# Print the given original string.
print("The given original string :", gvn_str)
# Print the given string after applying the lower() method.
print("The given string after applying lower() method :")
print(rslt)

Output:

The given original string : HELLO this IS Btechgeeks
The given string after applying lower() method :
hello this is btechgeeks

How does lower() function work in a program?

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.
  • Converting both the given first and second strings into lowercase using the lower() method and checking if both the strings are the same or not using the if conditional statement.
  • If it is true, then print “The given first and second strings are same”.
  • Else print “The given first and second strings are NOT same”.
  • The Exit of the program.

Below is the implementation:

# Give the first string as static input and store it in a variable.
gvn_fststr = "WELCOME to PythoN PROGRAMS"
# Give the second string as static input and store it in a variable.
gvn_scndstr = "Welcome to Python programs"
# Converting both the first and second strings into lowercase using the lower()
# method and checking if both the strings are same or not using the if conditional
# statement.
if(gvn_fststr.lower() == gvn_scndstr.lower()):
    # If it is true, then print "The given first and second strings are same".
    print("The given first and second strings are same")
else:
    # Else print "The given first and second strings are NOT same".
    print("The given first and second strings are NOT same")

Output:

The given first and second strings are same

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 lower() method to the given string returns a string with all characters in lower case.
  • Store it in another variable.
  • Print the given original string.
  • Print the given string after applying the lower() method.
  • 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 = ")
# Apply lower() method to the given string that returns a string with all characters
# in lower case.
# Store it in another variable.
rslt = gvn_str.lower()
# Print the given original string.
print("The given original string :", gvn_str)
# Print the given string after applying the lower() method.
print("The given string after applying lower() method :")
print(rslt)

Output:

Enter some Random String = GooD MORNING btechgeeks@123$$#
The given original string : GooD MORNING btechgeeks@123$$#
The given string after applying lower() method :
good morning btechgeeks@123$$#

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

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 »