Author name: Vikram Chiluka

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 »

Program for Python String swapcase() Function

In the previous article, we have discussed Program for Python String title() Function
Python String swapcase() Function:

The swapcase() method returns a string in which all upper case letters are converted to lower case and vice versa.

Syntax:

string.swapcase()

Parameters: This function doesn’t have any parameters.

Examples:

Example1:

Input:

Given first string = "welcome to PYTHON-Programs"
Given second string = "HELLO ALL GOOD MORNING"

Output:

The above given first string is : welcome to PYTHON-Programs
The given first string after applying swapcase() function:
WELCOME TO python-pROGRAMS
The above given second string is : HELLO ALL GOOD MORNING
The given second string after applying swapcase() function:
hello all good morning

Example2:

Input:

Given first string = "hello ALL gOOD MORNing"
Given second string = "HELLO this is BTECHGEEKS"

Output:

The above given first string is : hello ALL gOOD MORNing
The given first string after applying swapcase() function:
HELLO all Good mornING
The above given second string is : HELLO this is BTECHGEEKS
The given second string after applying swapcase() function:
hello THIS IS btechgeeks

Program for String swapcase() Function in Python

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

Approach:

  • Give the first string as static input and store it in a variable.
  • Apply swapcase() function to the given first string that returns a string in which all upper case letters are converted to lower case and vice versa.
  • Store it in another variable.
  • Print the above-given String.
  • Print the above-given String after applying the swapcase() function.
  • Similarly, do the same for the other string and print the result string.
  • The Exit of Program.

Below is the implementation:

# Give the first string as static input and store it in a variable.
gvn_fststr = "welcome to PYTHON-Programs"
# Apply swapcase() function to the given first string that returns a string
# in which all upper case letters are converted to lower case and vice versa.
# Store it in another variable.
rslt_str1 = gvn_fststr.swapcase()
# Print the above given String
print("The above given first string is :", gvn_fststr)
# Print the above given String after applying swapcase() function.
print("The given first string after applying swapcase() function:")
print(rslt_str1)
# Similarly do the same for other string and print the result string.
gvn_scndstr = "HELLO ALL GOOD MORNING"
rslt_str2 = gvn_scndstr.swapcase()
print("The above given second string is :", gvn_scndstr)
print("The given second string after applying swapcase() function:")
print(rslt_str2)

Output:

The above given first string is : welcome to PYTHON-Programs
The given first string after applying swapcase() function:
WELCOME TO python-pROGRAMS
The above given second string is : HELLO ALL GOOD MORNING
The given second string after applying swapcase() function:
hello all good morning

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.
  • Apply swapcase() function to the given first string that returns a string in which all upper case letters are converted to lower case and vice versa.
  • Store it in another variable.
  • Print the above-given String.
  • Print the above-given String after applying the swapcase() function.
  • Similarly, do the same for the other string and print the result string.
  • 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 = ")
# Apply swapcase() function to the given first string that returns a string
# in which all upper case letters are converted to lower case and vice versa.
# Store it in another variable.
rslt_str1 = gvn_fststr.swapcase()
# Print the above given String
print("The above given first string is :", gvn_fststr)
# Print the above given String after applying swapcase() function.
print("The given first string after applying swapcase() function:")
print(rslt_str1)
# Similarly do the same for other string and print the result string.
gvn_scndstr = input("Enter some Random String = ")
rslt_str2 = gvn_scndstr.swapcase()
print("The above given second string is :", gvn_scndstr)
print("The given second string after applying swapcase() function:")
print(rslt_str2)

Output:

Enter some Random String = hello ALL gOOD MORNing
The above given first string is : hello ALL gOOD MORNing
The given first string after applying swapcase() function:
HELLO all Good mornING
Enter some Random String = HELLO this is BTECHGEEKS
The above given second string is : HELLO this is BTECHGEEKS
The given second string after applying swapcase() function:
hello THIS IS btechgeeks

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.

Program for Python String swapcase() Function Read More »

Python String title() Function with Examples

In the previous article, we have discussed Python String split() Method Examples
Python String title() Function:

The title() method returns a string in which the first character of each word is capitalized. As in a header or a title.

If the word contains a number or a symbol, the following letter will be converted to upper case.

Syntax:

string.title()

Parameters: This function doesn’t have any parameters.

Examples:

Example1:

Input:

Given first string =  "hello this is btechgeeks"
Given second string = "10345 good morning 2a2a btechgeeks"

Output:

The above given first string is : hello this is btechgeeks
The given first string after applying title() function:
Hello This Is Btechgeeks
The above given second string is : 10345 good morning 2a2a btechgeeks
The given second string after applying title() function:
10345 Good Morning 2A2A Btechgeeks

Note:

It is worth noting that the first letter after a non-alphabet 
letter is converted to an upper case letter.

Example2:

Input:

Given first string = "welcome to Python-Programs"
Given second string = "HELLO ALL GOOD MORNING"

Output:

The above given first string is : welcome to Python-Programs
The given first string after applying title() function:
Welcome To Python-Programs
The above given second string is : HELLO ALL GOOD MORNING
The given second string after applying title() function:
Hello All Good Morning

Program for String title() Function in Python

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

Approach:

  • Give the first string as static input and store it in a variable.
  • Apply title() function to the given first string that returns a string in which the first character of each word is capitalized. As in a header or a title.
  • Store it in another variable.
  • Print the above-given String.
  • Print the above-given String after applying the title() function.
  • Similarly, do the same for the other string and print the result string.
  • The Exit of Program.

Below is the implementation:

# Give the first string as static input and store it in a variable.
gvn_fststr = "hello this is btechgeeks"
# Apply title() function to the given first string that returns a string in
# which the first character of each word is capitalized. As in a header or a title.
# Store it in another variable.
rslt_str1 = gvn_fststr.title()
# Print the above given String
print("The above given first string is :", gvn_fststr)
# Print the above given String after applying title() function.
print("The given first string after applying title() function:")
print(rslt_str1)
# Similarly do the same for other string and print the result string.
gvn_scndstr = "10345 good morning 2a2a btechgeeks"
rslt_str2 = gvn_scndstr.title()
print("The above given second string is :", gvn_scndstr)
print("The given second string after applying title() function:")
print(rslt_str2)

Output:

The above given first string is : hello this is btechgeeks
The given first string after applying title() function:
Hello This Is Btechgeeks
The above given second string is : 10345 good morning 2a2a btechgeeks
The given second string after applying title() function:
10345 Good Morning 2A2A Btechgeeks

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.
  • Apply title() function to the given first string that returns a string in which the first character of each word is capitalized. As in a header or a title.
  • Store it in another variable.
  • Print the above-given String.
  • Print the above-given String after applying the title() function.
  • Similarly, do the same for the other string and print the result string.
  • 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 = ")
# Apply title() function to the given first string that returns a string in
# which the first character of each word is capitalized. As in a header or a title.
# Store it in another variable.
rslt_str1 = gvn_fststr.title()
# Print the above given String
print("The above given first string is :", gvn_fststr)
# Print the above given String after applying title() function.
print("The given first string after applying title() function:")
print(rslt_str1)
# Similarly do the same for other string and print the result string.
gvn_scndstr = input("Enter some Random String = ")
rslt_str2 = gvn_scndstr.title()
print("The above given second string is :", gvn_scndstr)
print("The given second string after applying title() function:")
print(rslt_str2)

Output:

Enter some Random String = welcome to Python-Programs
The above given first string is : welcome to Python-Programs
The given first string after applying title() function:
Welcome To Python-Programs
Enter some Random String = HELLO ALL GOOD MORNING
The above given second string is : HELLO ALL GOOD MORNING
The given second string after applying title() function:
Hello All Good Morning

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 title() Function with Examples Read More »

Python String split() Method Examples

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

Splitting a string into a list is accomplished by the split() method.

The separator can be specified; the default separator is any whitespace.

Note: When maxsplit is specified, the list will have the number of elements specified plus one.

Syntax:

string.split(separator, maxsplit)

Parameters

separator: This is Optional. This parameter specifies the separator to be used when splitting the string. By default, any whitespace serves as a separator.

maxsplit: This is Optional. Specifies the number of splits to perform. The default value is -1, which represents “all occurrences.”

Examples:

Example1:

Input:

Given string = "welcome,to,python,programs"
Given separator = ","
Given maxsplit value = 1

Output:

The above given string is : welcome,to,python,programs
The given string after applying split() function:
['welcome', 'to,python,programs']

Example2:

Input:

Given string = "good morning this is btechgeeks"

Output:

The above given string is : good morning this is btechgeeks
The given string after applying split() function:
['good', 'morning', 'this', 'is', 'btechgeeks']

String split() 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 separator as static input and store it in another variable.
  • Give the maxsplit as static input and store it in another variable.
  • Apply split() method to the given string for the given separator and maxsplit() values which split a string into a list.
  • Store it in another variable.
  • Print the above-given string.
  • Print the above-given string after applying the split() function.
  • Similarly, do the same for the other string without giving the separator and maxsplit values and print the result list obtained.
  • The Exit of 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 separator as static input and store it in another variable.
gvn_separtr = ","
# Give the maxsplit as static input and store it in another variable.
maxsplt_valu = 1
# Apply split() method to the given string for the given separator and
# maxsplit() values which split a string into a list.
# Store it in another variable.
rslt_lst1 = gvn_fststr.split(gvn_separtr, maxsplt_valu)
# Print the above given string
print("The above given first string is :", gvn_fststr)
# Print the above given string after applying split() function.
print("The given first string after applying split() function:")
print(rslt_lst1)
# Similarly do the same for other string without giving the separator and
# maxsplit values and print the result list obtained.
gvn_scndstr = "good morning this is btechgeeks"
rslt_lst2 = gvn_scndstr.split()
print("The above given second string is :", gvn_scndstr)
print("The given second string after applying split() function:")
print(rslt_lst2)

Output:

The above given first string is : welcome,to,python,programs
The given first string after applying split() function:
['welcome', 'to,python,programs']
The above given second string is : good morning this is btechgeeks
The given second string after applying split() function:
['good', 'morning', 'this', 'is', 'btechgeeks']

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 separator as user input using the input() function and store it in another variable.
  • Give the maxsplit as user input using the int(input()) function and store it in another variable.
  • Apply split() method to the given string for the given separator and maxsplit() values which split a string into a list.
  • Store it in another variable.
  • Print the above-given string.
  • Print the above-given string after applying split() function.
  • Similarly, do the same for the other string without giving the separator and maxsplit values and print the result list obtained.
  • 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 separator as user input using the input() function and store it in another variable.
gvn_separtr = input("Enter some random separator = ")
# Give the maxsplit as user input using the int(input()) function and store it in another variable. 
maxsplt_valu = int(input("Enter some random number = "))
# Apply split() method to the given string for the given separator and
# maxsplit() values which split a string into a list.
# Store it in another variable.
rslt_lst1 = gvn_fststr.split(gvn_separtr, maxsplt_valu)
# Print the above given string
print("The above given first string is :", gvn_fststr)
# Print the above given string after applying split() function.
print("The given first string after applying split() function:")
print(rslt_lst1)
# Similarly do the same for other string without giving the separator and
# maxsplit values and print the result list obtained.
gvn_scndstr = input("Enter some random string = ")
rslt_lst2 = gvn_scndstr.split()
print("The above given second string is :", gvn_scndstr)
print("The given second string after applying split() function:")
print(rslt_lst2)

Output:

Enter some random string = hello#this is btechgeeks
Enter some random separator = #
Enter some random number = 1
The above given first string is : hello#this is btechgeeks
The given first string after applying split() function:
['hello', 'this is btechgeeks']
Enter some random string = hello all good morning
The above given second string is : hello all good morning
The given second string after applying split() function:
['hello', 'all', 'good', 'morning']

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

Python ord() Function with Examples

In the previous article, we have discussed Python Program for round() Function
ord() Function in Python:

The ord() function returns a number that represents the Unicode code of a given character.

Syntax:

ord(character)

Parameters

character: It is a Unicode character.

Return Value:

The ord() function returns the Unicode character as an integer.

Note: When the length of the string is not equal to one, a TypeError is thrown.

Examples:

Example1:

Input:

Given character = '4'
Given character = '$'
Given character = 'd'

Output:

The integer representing the given Unicode character 4 =  52
The integer representing the given Unicode character $ = 36
The integer representing the given Unicode character d = 100

Example2:

Input:

Given character = '0'
Given character = '@'
Given character = 'y'

Output:

The integer representing the given Unicode character 0 =  48
The integer representing the given Unicode character @ = 64
The integer representing the given Unicode character y = 121

Program for ord() Function in Python

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

Approach:

  • Give the character as static input and store it in a variable.
  • Apply ord() function to the given character which returns a number that represents the Unicode code of a given character.
  • Store it in another variable.
  • Print the integer representing the above given Unicode character.
  • The Exit of Program.

Below is the implementation:

# Give the character as static input and store it in a variable.
gvn_charctr = '4'
# Apply ord() function to the given character which returns a number that
# represents the Unicode code of a given character.
# Store it in another variable.
charctrs_unicode = ord(gvn_charctr)
# Print the integer representing the above given Unicode character.
print("The integer representing the given Unicode character",
      gvn_charctr, "= ", charctrs_unicode)

Output:

The integer representing the given Unicode character 4 =  52

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

Approach:

  • Give the character as user input using the input() function and store it in a variable.
  • Apply ord() function to the given character which returns a number that represents the Unicode code of a given character.
  • Store it in another variable.
  • Print the integer representing the above given Unicode character.
  • The Exit of Program.

Below is the implementation:

# Give the character as user input using the input() function and store it in a variable.
gvn_charctr = input("Enter some random character = ")
# Apply ord() function to the given character which returns a number that
# represents the Unicode code of a given character.
# Store it in another variable.
charctrs_unicode = ord(gvn_charctr)
# Print the integer representing the above given Unicode character.
print("The integer representing the given Unicode character",
      gvn_charctr, "= ", charctrs_unicode)

Output:

Enter some random character = B
The integer representing the given Unicode character B = 66

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

Python ord() Function with Examples Read More »

Python memoryview() Function with Examples

In the previous article, we have discussed Python Program for ord() Function
The memoryview() function in Python returns memory views objects. Before delving deeper into the memoryview() function, consider why we use it.

Why are we using the memoryview() function?
Because Memory view is a safe way to expose the buffer protocol in Python and a memoryview behaves like bytes in many useful contexts (for example, it supports the mapping protocol), it can serve as an adequate replacement if used correctly. The best part is that it uses the buffer protocol under the hood to avoid copies and simply juggle pointers to data. So, before we get into what memory views are available, we must first understand the Buffer Protocol.

What is buffer protocol?

The buffer protocol allows you to access an object’s internal data. This internal data is in the form of a memory array or a buffer. It enables one object to expose its internal data (buffers) and another to access those buffers without requiring intermediate copying. The buffer protocol is only available to us at the C-API level, not through our normal codebase. Memory views are used to expose the same protocol to a standard Python codebase.

Memoryview

Memoryview objects enable Python code to access the internal data of an object that supports the buffer protocol without the need for copying. The memoryview() function provides direct read and write access to byte-oriented data in an object without the need to copy it first. Because it does not create a copy when slicing, this can result in significant performance gains when working with large objects.

Syntax:

memoryview(object)

Parameters

object: The object whose internal data will be exposed.
buffer protocol support – str and bytearray (but not Unicode).

Return Value:

A memoryview object is returned.

Examples:

Example1:

Input:

Given String = 'EFG'

Output:

69
b'EFG'
[69, 70, 71]

Example2:

Input:

Given String = 'uvw'

Output:

117
b'uvw'
[117, 118, 119]

Program for memoryview() Function in Python

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

Approach:

  • Take a random string and pass it to the bytearray() function with the second argument as ‘utf-8’ and store it in a variable.
  • Calculate the memoryview of the above bytearray by passing the above bytearray to memoryview () function.
  • Store it in another variable.
  • Print or access the zeroth index of the memoryview using the memoryview[0].
  • Create the byte from the memoryview by passing memoryview with slicing to bytes() function and print it.
  • Create a list from the memoryview by passing the memoryview with slicing and convert it into list using the list() function and print it.
  • The Exit of the Program.

Below is the implementation:

# Take a random string and pass it to the bytearray() function with the
# second argument as 'utf-8' and store it in a variable.
randmbyte_arry = bytearray('EFG', 'utf-8')
# Calculate the memoryview of the above bytearray by passing the above
# bytearray to memoryview () function.
# store it in another variable.
memryvieww = memoryview(randmbyte_arry)
# Print or access the zeroth index of the memoryview using the memoryview[0].
print(memryvieww[0])
# Create the byte from the memoryview by passing memoryview with slicing to
# bytes() function and print it.
print(bytes(memryvieww[0:3]))
# Create a list from the memoryview by passing the memoryview with slicing
# and convert it into list using the list() function and print it.
print(list(memryvieww[0:5]))

Output:

69
b'EFG'
[69, 70, 71]

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.
  • Take the above random string and pass it to the bytearray() function with the second argument as ‘utf-8’ and store it in a variable.
  • Calculate the memoryview of the above bytearray by passing the above bytearray to memoryview () function.
  • Store it in another variable.
  • Print or access the zeroth index of the memoryview using the memoryview[0].
  • Create the byte from the memoryview by passing memoryview with slicing to bytes() function and print it.
  • Create a list from the memoryview by passing the memoryview with slicing and convert it into list using the list() function and print it.
  • 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 = ")
# Take the above random string and pass it to the bytearray() function with the
# second argument as 'utf-8' and store it in a variable.
randmbyte_arry = bytearray(gvn_str, 'utf-8')
# Calculate the memoryview of the above bytearray by passing the above
# bytearray to memoryview () function.
# store it in another variable.
memryvieww = memoryview(randmbyte_arry)
# Print or access the zeroth index of the memoryview using the memoryview[0].
print(memryvieww[0])
# Create the byte from the memoryview by passing memoryview with slicing to
# bytes() function and print it.
print(bytes(memryvieww[0:3]))
# Create a list from the memoryview by passing the memoryview with slicing
# and convert it into list using the list() function and print it.
print(list(memryvieww[0:5]))

Output:

Enter some random string = uvw
117
b'uvw'
[117, 118, 119]

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

 

Python memoryview() Function with Examples Read More »

Python Program to Find Index of a Tuple Item

In the previous article, we have discussed Python Program for memoryview() Function
The tuple index function in Python returns the index of a given tuple item. The index position of the first found value is returned by the tuple index function.

Syntax:

TupleName.index(tupleValue, start, end)

If you specify a start value, the index function will begin searching from that point. Similarly, setting the end position causes the tuple index function to stop looking at that number.

Examples:

Example1:

Input:

Given tuple = (10, 20, 30, 50, 70, 20)
Given first number = 50
Given second number = 20
Given start position = 2

Output:

The given number's 50 index position =  3
The given number's 20 index position after the given start position 2 =  5

Example2:

Input:

Given tuple = (1, 3, 5, 7, 8, 2, 5)
Given first number = 3
Given second number = 5
Given start position = 3

Output:

The given number's 3 index position =  1
The given number's 5 index position after the given start position 3 =  6

Program to Find Index of a Tuple Item in Python

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

Approach:

  • Give the tuple as static input and store it in a variable.
  • Give the number as static input and store it in another variable.
  • Get the index position of the given number using the index() method and store it in another variable.
  • Print the index position of the given number.
  • Give the second number as static input and store it in another variable.
  • Give the start position as static input and store it in another variable.
  • Pass the given second number, start position as arguments to the index() method to get the index value of the given second number after the given start position.
  • Store it in another variable.
  • Print the index position of the given second number after the given start position.
  • The Exit of Program.

Below is the implementation:

# Give the tuple as static input and store it in a variable.
gvn_tupl = (10, 20, 30, 50, 70, 20)
# Give the number as static input and store it in another variable.
gvn_numb1 = 50
# Get the index position of the given number using the index() method and
# store it in another variable.
indx_1 = gvn_tupl.index(gvn_numb1)
# Print the index position of the given number.
print("The given number's", gvn_numb1, "index position = ", indx_1)
# Give the second number as static input and store it in another variable.
gvn_numb2 = 20
# Give the start position as static input and store it in another variable.
gvn_strtpositn = 2
# Pass the given second number, start position as arguments to the index()
# method to get the index value of the given second number after the given
# start position.
# Store it in another variable.
indx_2 = gvn_tupl.index(gvn_numb2, gvn_strtpositn)
# Print the index position of the given second number after the given
# start position.
print("The given number's", gvn_numb2,
      "index position after the given start position", gvn_strtpositn, "= ", indx_2)

Output:

The given number's 50 index position =  3
The given number's 20 index position after the given start position 2 =  5

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

Approach:

  • Give the tuple user input using list(),map(),input(),and split() functions and store it in a variable.
  • Give the number as user input using the int(input()) function and store it in another variable.
  • Get the index position of the given number using the index() method and store it in another variable.
  • Print the index position of the given number.
  • Give the second number as user input using the int(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.
  • Pass the given second number, start position as arguments to the index() method to get the index value of the given second number after the given start position.
  • Store it in another variable.
  • Print the index position of the given second number after the given start position.
  • The Exit of Program.

Below is the implementation:

# Give the tuple user input using list(),map(),input(),and split() functions and store it in a variable.
gvn_tupl = tuple(map(int, input(
    'Enter some random Tuple Elements separated by spaces = ').split()))
# 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 = "))
# Get the index position of the given number using the index() method and
# store it in another variable.
indx_1 = gvn_tupl.index(gvn_numb1)
# Print the index position of the given number.
print("The given number's", gvn_numb1, "index position = ", indx_1)
# Give the second number as user input using the int(input()) function and
# store it in another variable.
gvn_numb2 =  int(input("Enter some random number = "))
# 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 = "))
# Pass the given second number, start position as arguments to the index()
# method to get the index value of the given second number after the given
# start position.
# Store it in another variable.
indx_2 = gvn_tupl.index(gvn_numb2, gvn_strtpositn)
# Print the index position of the given second number after the given
# start position.
print("The given number's", gvn_numb2,
      "index position after the given start position", gvn_strtpositn, "= ", indx_2)

Output:

Enter some random Tuple Elements separated by spaces = 1 3 5 7 8 2 5
Enter some random number = 3
The given number's 3 index position = 1
Enter some random number = 5
Enter some random number = 3
The given number's 5 index position after the given start position 3 = 6

Program to Find Index of a Tuple Item in Python without using index() Function

Below is the implementation:

gvn_tupl = (10, 40, 50, 30, 60, 40, 20, 40, 30)
print(gvn_tupl)

gvn_valu = 40
for itr in range(len(gvn_tupl)):
    if gvn_tupl[itr] == gvn_valu:
        print("The given number", gvn_valu, "index position = ", itr)
        break
print("The all Index Positions of given number:")
for k in range(len(gvn_tupl)):
    if gvn_tupl[k] == gvn_valu:
        print("Index Position of 20 = ", k)

print("The all the Index Positions of given number using enumerate function:")
for itor, p in enumerate(gvn_tupl):
    if p == gvn_valu:
        print("Index Position of 20 = ", itor)

Output:

(10, 40, 50, 30, 60, 40, 20, 40, 30)
The given number 40 index position =  1
The all Index Positions of given number:
Index Position of 20 =  1
Index Position of 20 =  5
Index Position of 20 =  7
The all the Index Positions of given number using enumerate function:
Index Position of 20 =  1
Index Position of 20 =  5
Index Position of 20 =  7

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

Python Program to Find Index of a Tuple Item Read More »

Python reversed() Function with Examples

In the previous article, we have discussed Python Program for chr() Function
reversed() Function in Python:

The reversed() function returns an iterator object that has been reversed.

Syntax:

reversed(sequence)

Parameters

sequence: This is required.Any object that is iterable. (list,tuple..etc)

A sequence is an object that implements the sequence protocols __len__() and __getitem__(). For instance, tuple, string, list, range, and so on.

In addition, we can use reversed() in any object that implements __reverse__ ().

Return Value:

The reversed() function returns an iterator that iterates through the given sequence in reverse order.

Examples:

Example1:

Input:

Given string = 'Python-programs'
Given tuple = ("h", "e", "l", "l", "o")

Output:

['s', 'm', 'a', 'r', 'g', 'o', 'r', 'p', '-', 'n', 'o', 'h', 't', 'y', 'P']
['o', 'l', 'l', 'e', 'h']

Note: After applying the reverse function the result is converted into the list.Hence the result is in the list form.

Example2:

Input:

Given list =  [6, 7, 8, 9, 10]
Given range = (2, 12)

Output:

[10, 9, 8, 7, 6]
[11, 10, 9, 8, 7, 6, 5, 4, 3, 2]

Program for reversed() Function in Python

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

Approach:

  • Give the string as static input and store it in a variable.
  • Get the reverse of a string by applying the reversed() function to the given string and convert it into a list using the list() function.
  • Print the result.
  • Give the tuple as static input and store it in another variable.
  • Get the reverse of a tuple by applying the reversed() function to the given tuple and convert it into a list using the list() function.
  • Print the result.
  • Give the list as static input and store it in another variable.
  • Get the reverse of a list by applying the reversed() function to the given list and convert it into a list using the list() function.
  • Print the result.
  • Give the lower and upper limits as arguments to the range() function and store it in a variable.
  • Get the reverse order of the range by applying the reversed() function to the given range and convert it into a list using the list() function.
  • Print the result.
  • The Exit of Program.

Below is the implementation:

# Give the string as static input and store it in a variable.
gvn_str = 'Python-programs'
# Get the reverse of a string by applying the reversed() function to the
# given string and convert it into a list using the list() function.
# Print the result.
print(list(reversed(gvn_str)))
# Give the tuple as static input and store it in another variable.
gvn_tupl = ("h", "e", "l", "l", "o")
# Get the reverse of a tuple by applying the reversed() function to the
# given tuple and convert it into a list using the list() function.
# Print the result.
print(list(reversed(gvn_tupl)))
# Give the list as static input and store it in another variable.
gvn_lst = [6, 7, 8, 9, 10]
# Get the reverse of a list by applying the reversed() function to the
# given list and convert it into a list using the list() function.
# Print the result.
print(list(reversed(gvn_lst)))
# Give the lower and upper limits as arguments to the range()function and
# store it in a variable.
gvn_rangee = range(2, 12)
# Get the reverse order of the range by applying the reversed() function
# to the given range and convert it into a list using the list() function.
# Print the result.
print(list(reversed(gvn_rangee)))

Output:

['s', 'm', 'a', 'r', 'g', 'o', 'r', 'p', '-', 'n', 'o', 'h', 't', 'y', 'P']
['o', 'l', 'l', 'e', 'h']
[10, 9, 8, 7, 6]
[11, 10, 9, 8, 7, 6, 5, 4, 3, 2]

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.
  • Get the reverse of a string by applying the reversed() function to the given string and convert it into a list using the list() function.
  • Print the result.
  • Give the tuple user input using list(),map(),input(),and split() functions and store it in another variable.
  • Get the reverse of a tuple by applying the reversed() function to the given tuple and convert it into a list using the list() function.
  • Print the result.
  • Give the list as user input using list(),map(),input(),and split() functions and store it in a variable.
  • Get the reverse of a list by applying the reversed() function to the given list and convert it into a list using the list() function.
  • Print the result.
  • Give the lower and upper limits as user input using the int(input()) function and store them in two separate variables.
  • Pass the given lower and upper as arguments to the range() function and store it in a variable.
  • Get the reverse order of the range by applying the reversed() function to the given range and convert it into a list using the list() function.
  • 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_str = input("Enter some random string = ")
# Get the reverse of a string by applying the reversed() function to the
# given string and convert it into a list using the list() function.
# Print the result.
print(list(reversed(gvn_str)))
# Give the tuple user input using list(),map(),input(),and split() functions and
# store it in another variable.
gvn_tupl=tuple(map(int, input(
    'Enter some random Tuple Elements separated by spaces = ').split()))
# Get the reverse of a tuple by applying the reversed() function to the
# given tuple and convert it into a list using the list() function.
# Print the result.
print(list(reversed(gvn_tupl)))
# Give the list as user input using list(),map(),input(),and split() functions and
# store it in a variable.
gvn_lst=list(map(int, input(
    'Enter some random List Elements separated by spaces = ').split()))
# Get the reverse of a list by applying the reversed() function to the
# given list and convert it into a list using the list() function.
# Print the result.
print(list(reversed(gvn_lst)))
# Give the lower and upper limits as user input using the int(input()) function and
# store them in two separate variables.
gvn_lowrlmt=int(input("Enter some random number = "))
gvn_upprlmt=int(input("Enter some random number = "))
# Pass the given lower and upper as arguments to the range() function and store it in a variable.
gvn_rangee=range(gvn_lowrlmt, gvn_upprlmt)
# Get the numbers in reverse order by applying the reversed() function to the
# given range and convert it into a list using the list() function.
# Print the result.
print(list(reversed(gvn_rangee)))

Output:

Enter some random string = good morning
['g', 'n', 'i', 'n', 'r', 'o', 'm', ' ', 'd', 'o', 'o', 'g']
Enter some random Tuple Elements separated by spaces = 0 4 5 1 2
[2, 1, 5, 4, 0]
Enter some random List Elements separated by spaces = 7 4 2 6
[6, 2, 4, 7]
Enter some random number = 15
Enter some random number = 22
[21, 20, 19, 18, 17, 16, 15]

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

Python reversed() Function with Examples Read More »