Python

Python List clear() Method with Examples

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

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

List clear() Method in Python:

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

Syntax:

list.clear()

Parameter Values: This method has no parameters.

Return Value:

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

Examples:

Example1:

Input:

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

Output:

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

Example2:

Input:

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

Output:

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

List clear() Method with Examples in Python

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

Approach:

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

Below is the implementation:

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

Output:

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

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

Approach:

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

Below is the implementation:

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

Output:

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

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

Using del to clear the list

Approach:

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

Below is the implementation:

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

Output:

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

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

Python List clear() Method with Examples Read More »

Python List append() Method with Examples

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

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

List append() Method in Python:

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

Syntax:

list.append(item)

Parameters

The method only accepts one argument.

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

Return Value:

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

Examples:

Example1:

Input:

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

Output:

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

Example2:

Input:

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

Output:

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

List append() Method with Examples in Python

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

1)Appending an item to List

Approach:

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

Below is the implementation:

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

Output:

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

Approach:

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

Below is the implementation:

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

Output:

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

Approach:

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

Below is the implementation:

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

Output:

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

Approach:

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

Below is the implementation:

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

Output:

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

 

Python List append() Method with Examples Read More »

Python String islower() Method with Examples

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

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

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

Syntax:

string.islower()

Parameters: This function has no parameters.

Return Value:

The islower() method gives:

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

Examples:

Example1:

Input:

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

Output:

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

Example2:

Input:

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

Output:

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

String islower() Method Examples in Python

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

Approach:

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

Below is the implementation:

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

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

Output:

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

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

Approach:

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

Below is the implementation:

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

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

Output:

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

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

Python String islower() Method with Examples Read More »

Python String isnumeric() Method with Examples

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

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

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

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

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

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

Syntax:

string.isnumeric()

Parameters: This function has no parameters.

Return Value:

The isnumeric() method gives:

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

Examples:

Example1:

Input:

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

Output:

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

Example2:

Input:

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

Output:

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

String isnumeric() Method Examples in Python

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

Approach:

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

Below is the implementation:

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

Output:

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

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

Approach:

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

Below is the implementation:

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

Output:

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

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

Python String isnumeric() Method with Examples Read More »

Python String zfill() Method with Examples

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

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

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

Syntax:

string.zfill(len)

Parameters

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

Return Value:

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

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

Examples:

Example1:

Input:

Given string = "hello btechgeeks"
Given number = 10

Output:

hello btechgeeks

Example2:

Input:

Given string = "hello btechgeeks"
Given number = 25

Output:

000000000hello btechgeeks

String zfill() Method with Examples in Python

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

Approach:

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

Below is the implementation:

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

Output:

0000000035

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

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

Output:

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

Output:

00020.55

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

Approach:

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

Below is the implementation:

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

Output:

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

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

Below is the implementation:

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

Output:

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

Below is the implementation:

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

Output:

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

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

Python String zfill() Method with Examples Read More »

Python String isupper() Method with Examples

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

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

Syntax:

string.isupper()

Parameters: This function has no parameters.

Return Value:

The isupper() method gives:

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

Examples:

Example1:

Input:

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

Output:

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

Example2:

Input:

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

Output:

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

String isupper() Method Examples in Python

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

Approach:

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

Below is the implementation:

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

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

Output:

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

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

Approach:

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

Below is the implementation:

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

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

Output:

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

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

Python String isupper() Method with Examples Read More »

Python String istitle() Method with Examples

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

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

Syntax:

string.istitle()

Parameters: This function has no parameters.

Return Value:

The istitle() method gives:

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

Examples:

Example1:

Input:

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

Output:

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

Example2:

Input:

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

Output:

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

String istitle() Method Examples in Python

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

Approach:

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

Below is the implementation:

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

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

Output:

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

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

Approach:

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

Below is the implementation:

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

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

Output:

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

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

Python String istitle() Method with Examples Read More »

Python String isspace() Method with Examples

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

If the string contains only whitespace characters, the isspace() method returns True. If it does not, it returns False.

Whitespace characters are characters that are used for spacing. For example, tabs, spaces, newlines, and so on.

Syntax:

string.isspace()

Parameters: This function has no parameters.

Return Value:

The isspace() method gives:

  • True if the string contains only whitespace characters.
  • If the string is empty or contains at least one non-printable character, it returns False.

Examples:

Example1:

Input:

Given string = ' Python-programs '
Given string = ' \t'
Given string = ''

Output:

The given string =    Python-programs  
Checking if the given string is has whitespace or not =  False
The given string =      	
Checking if the given string is whitespace or not =  True
The given string =  
Checking if the given string is whitespace or not =  False

Example2:

Input:

Given string = 'welcome to python programs'
Given string = '\t \t'
Given string = 'hellobtechgeeks'

Output:

The given string =  welcome to python programs
Checking if the given string is has whitespace or not =  False
The given string =  	  	
Checking if the given string is whitespace or not =  True
The given string =  hellobtechgeeks
Checking if the given string is whitespace or not =  False

String isspace() Method Examples in Python

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

Approach:

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

Below is the implementation:

# Give the string as static input and store it in a variable.
gvn_strng1 = '  Python-programs  '
# Apply isspace() function to the given string in which if the string
# contains only whitespace characters, the isspace() method returns True.
# If it does not, it returns False.
# Store it in another variable.
rslt_1 = gvn_strng1.isspace()
# Print the above-given String.
print("The given string = ", gvn_strng1)
# Print the result after checking if the given string has only whitespace
# characters or not.
print("Checking if the given string is has whitespace or not = ", rslt_1)
# Similarly, do the same for other strings and print the result.
gvn_strng2 = '    \t'
rslt_2 = gvn_strng2.isspace()
print("The given string = ", gvn_strng2)
print("Checking if the given string is whitespace or not = ", rslt_2)

gvn_strng3 = ''
rslt_3 = gvn_strng3.isspace()
print("The given string = ", gvn_strng3)
print("Checking if the given string is whitespace or not = ", rslt_3)

Output:

The given string =    Python-programs  
Checking if the given string is has whitespace or not =  False
The given string =      	
Checking if the given string is whitespace or not =  True
The given string =  
Checking if the given string is whitespace or not =  False

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

Python String isspace() Method with Examples Read More »

Python String isprintable() Method with Examples

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

Python String isprintable() is a built-in string handling method. If all characters in the string are printable or the string is empty, the isprintable() method returns “True.” Otherwise, it returns “False.” This function determines whether or not the argument contains any printable characters, such as:

  • Digits or numbers ( 0123456789 )
  • Uppercase alphabets ( ABCDEFGHIJKLMNOPQRSTUVWXYZ )
  • Lowercase alphabets ( abcdefghijklmnopqrstuvwxyz )
  • Punctuation ( !”#$%&'()*+, -./:;?@[\]^_`{ | }~ )
  • whitespace ( )

Syntax:

string.isprintable()

Parameters: This function has no parameters.

Return Value:

This method returns:

  • True if the string is empty or if all of its characters are printable.
  • If the string contains at least one non-printable character, it returns False.

Examples:

Example1:

Input:

Given string = 'Welcome to Python-programs'
Given string = 'hello this is\n btechgeeks'
Given string = ' '

Output:

The given string =  Welcome to Python-programs
Checking if the given string is printable or not =  True
The given string =  hello this is
 btechgeeks
Checking if the given string is printable or not =  False
The given string =   
Checking if the given string is printable or not =  True

Example2:

Input:

Given string = '/n good morning btechgeeks\n'
Given string = 'hello this a python platform'
Given string = '_welcome all_'

Output:

The given string =  /n good morning btechgeeks

Checking if the given string is printable or not =  False
The given string =  hello this a python platform
Checking if the given string is printable or not =  True
The given string =  _welcome all_
Checking if the given string is printable or not =  True

String isprintable() Method Examples in Python

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

Approach:

  • Give the string as static input and store it in a variable.
  • Apply isprintable() function to the given string in which if all characters in the given string are printable or the string is empty, the isprintable() method returns “True.” Otherwise, it returns “False.”
  • Store it in another variable.
  • Print the above-given String.
  • Print the result after checking if the given string is printable or not.
  • Similarly, do the same for other strings and print the result.
  • The Exit of Program.

Below is the implementation:

# Give the string as static input and store it in a variable.
gvn_strng1 = 'Welcome to Python-programs'
# Apply isprintable() function to the given string in which if all characters
# in the given string are printable or the string is empty,
# the isprintable() method returns "True." Otherwise, it returns "False."
# Store it in another variable.
rslt_1 = gvn_strng1.isprintable()
# Print the above-given String.
print("The given string = ", gvn_strng1)
# Print the result after checking if the given string is printable or not.
print("Checking if the given string is printable or not = ", rslt_1)
# Similarly, do the same for other strings and print the result.
gvn_strng2 = 'hello this is\n btechgeeks'
rslt_2 = gvn_strng2.isprintable()
print("The given string = ", gvn_strng2)
print("Checking if the given string is printable or not = ", rslt_2)

gvn_strng3 = ' '
rslt_3 = gvn_strng3.isprintable()
print("The given string = ", gvn_strng3)
print("Checking if the given string is printable or not = ", rslt_3)

Output:

The given string =  Welcome to Python-programs
Checking if the given string is printable or not =  True
The given string =  hello this is
 btechgeeks
Checking if the given string is printable or not =  False
The given string =   
Checking if the given string is printable or not =  True

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

Python String isprintable() Method with Examples Read More »

Python String isidentifier() Method with Examples

In the previous article, we have discussed Python Program to Find Index of a Tuple Item
isidentifier() Method in Python:

If the string is a valid identifier, the isidentifier() method returns true; otherwise, it returns False.

If a string only contains alphanumeric letters (a-z) and numbers (0-9), it is considered a valid identifier. A valid identifier must not begin with a number and must not contain any spaces.

Syntax:

string.isidentifier()

Parameters: This function has no parameters.

Return Value:

The isidentifier() method gives:

  • If the string is a valid identifier, it returns true.
  • If the string is not an invalid identifier, it returns False.

Examples:

Example1:

Input:

Given first string = 'PythonPrograms'
Given second string = 'hello 123'
Given third string = '123welcome'
Given fourth string = ''

Output:

The given string =  PythonPrograms
Checking if the given string is a valid identifier or not =  True
The given string =  hello 123
Checking if the given string is a valid identifier or not =  False
The given string =  123welcome
Checking if the given string is a valid identifier or not =  False
The given string =  
Checking if the given string is a valid identifier or not =  False

Example2:

Input:

Given first string = 'Btechgeeks'
Given second string = 'Programs_python'
Given third string = 'welcome@btechgeeks'
Given fourth string = 'goodmorning123456'

Output:

The given string = Btechgeeks
Checking if the given string is a valid identifier or not = True
The given string = Programs_python
Checking if the given string is a valid identifier or not = True
The given string = welcome@btechgeeks
Checking if the given string is a valid identifier or not = False
The given string = goodmorning123456
Checking if the given string is a valid identifier or not = True

String isidentifier() Method Examples in Python

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

Approach:

  • Give the string as static input and store it in a variable.
  • Apply isidentifier() function to the given string that returns true if the given string is a valid identifier,  otherwise, it returns False.
  • Store it in another variable.
  • Print the above-given String.
  • Print the result after checking if the given string is a valid identifier or not.
  • Similarly, do the same for other strings and print the result.
  • The Exit of Program.

Below is the implementation:

# Give the string as static input and store it in a variable.
gvn_strng1 = 'PythonPrograms'
# Apply isidentifier() function to the given string that returns true if
# the given string is a valid identifier,  otherwise, it returns False.
# Store it in another variable.
rslt_1 = gvn_strng1.isidentifier()
# Print the above-given String.
print("The given string = ", gvn_strng1)
# Print the result after checking if the given string is a valid
# identifier or not.
print("Checking if the given string is a valid identifier or not = ", rslt_1)
# Similarly, do the same for other strings and print the result.
gvn_strng2 = 'hello 123'
rslt_2 = gvn_strng2.isidentifier()
print("The given string = ", gvn_strng2)
print("Checking if the given string is a valid identifier or not = ", rslt_2)

gvn_strng3 = '123welcome'
rslt_3 = gvn_strng3.isidentifier()
print("The given string = ", gvn_strng3)
print("Checking if the given string is a valid identifier or not = ", rslt_3)

gvn_strng4 = ''
rslt_4 = gvn_strng4.isidentifier()
print("The given string = ", gvn_strng4)
print("Checking if the given string is a valid identifier or not = ", rslt_4)

Output:

The given string =  PythonPrograms
Checking if the given string is a valid identifier or not =  True
The given string =  hello 123
Checking if the given string is a valid identifier or not =  False
The given string =  123welcome
Checking if the given string is a valid identifier or not =  False
The given string =  
Checking if the given string is a valid identifier or not =  False

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 isidentifier() function to the given string that returns true if the given string is a valid identifier,  otherwise, it returns False.
  • Store it in another variable.
  • Print the above-given String.
  • Print the result after checking if the given string is a valid identifier or not.
  • Similarly, do the same for other strings and print the result.
  • The Exit of Program.

Below is the implementation:

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

gvn_strng3 = input("Enter some Random String = ")
rslt_3 = gvn_strng3.isidentifier()
print("The given string = ", gvn_strng3)
print("Checking if the given string is a valid identifier or not = ", rslt_3)

gvn_strng4 = input("Enter some Random String = ")
rslt_4 = gvn_strng4.isidentifier()
print("The given string = ", gvn_strng4)
print("Checking if the given string is a valid identifier or not = ", rslt_4)

Output:

Enter some Random String = Btechgeeks
The given string = Btechgeeks
Checking if the given string is a valid identifier or not = True
Enter some Random String = Programs_python
The given string = Programs_python
Checking if the given string is a valid identifier or not = True
Enter some Random String = welcome@btechgeeks
The given string = welcome@btechgeeks
Checking if the given string is a valid identifier or not = False
Enter some Random String = goodmorning123456
The given string = goodmorning123456
Checking if the given string is a valid identifier or not = True

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

Python String isidentifier() Method with Examples Read More »