Author name: Vikram Chiluka

Python String rstrip() Method Examples

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

The rstrip() method removes any trailing characters (characters at the end of a string); the default trailing character to remove is space.

Syntax:

string.rstrip(characters)

Parameters

characters: This is optional. A set of characters that should be removed as trailing characters.

Examples:

Example1:

Input:

Given string = " pythonprograms,,,,,,,"
Given characters = ","

Output:

The above given string is :      pythonprograms,,,,,,,
The given string after applying rstrip() function:      pythonprograms

Example2:

Input:

Given string = "python-programs_!!!!!&&&&"
Given characters = "!&"

Output:

The above given string is : python-programs_!!!!!&&&&
The given string after applying rstrip() function: python-programs_

String rstrip() 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 character as static input and store it in another variable.
  • Apply rstrip() method to the given string for the given character to remove any trailing characters (characters at the end of a string); the default trailing character to remove is space.
  • Store it in another variable.
  • Print the above-given string
  • Print the above-given string after applying rstrip() function.
  • Similarly, do the same for other the string without giving the character 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 = "     pythonprograms,,,,,,,"
# Give the character as static input and store it in another variable.
gvn_chrctr = ","
# Apply rstrip() method to the given string for the given character to remove
# any trailing characters (characters at the end of a string); the default
# trailing character to remove is space.
# Store it in another variable.
rslt_str1 = gvn_fststr.rstrip(gvn_chrctr)
# Print the above given string
print("The above given first string is :", gvn_fststr)
# Print the above given string after applying rstrip() function.
print("The given first string after applying rstrip() function:", rslt_str1)
# Similarly do the same for other string without giving the character
# and print the result string.
gvn_scndstr = "?????good morning             "
rslt_str2 = gvn_scndstr.rstrip()
print("The above given second string is :", gvn_scndstr)
print("The given second string after applying rstrip() function:", rslt_str2)

Output:

The above given first string is :      pythonprograms,,,,,,,
The given first string after applying rstrip() function:      pythonprograms
The above given second string is : ?????good morning             
The given second string after applying rstrip() function: ?????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.
  • Give the character as user input using the input() function and store it in another variable.
  • Apply rstrip() method to the given string for the given character to remove any trailing characters (characters at the end of a string); the default trailing character to remove is space.
  • Store it in another variable.
  • Print the above-given string
  • Print the above-given string after applying rstrip() 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 = ")
# Give the character as user input using the input() function and store it in another variable.
gvn_chrctr = input("Enter some random character = ")
# Apply rstrip() method to the given string for the given character to remove
# any trailing characters (characters at the end of a string); the default
# trailing character to remove is space.
# Store it in another variable.
rslt_str1 = gvn_fststr.rstrip(gvn_chrctr)
# Print the above given string
print("The above given first string is :", gvn_fststr)
# Print the above given string after applying rstrip() function.
print("The given first string after applying rstrip() function:", rslt_str1)
# Similarly do the same for other string and print the result string.
gvn_scndstr = input("Enter some random string = ")
gvn_chrctr2 = input("Enter some random characters = ")
rslt_str2 = gvn_scndstr.rstrip(gvn_chrctr2)
print("The above given second string is :", gvn_scndstr)
print("The given second string after applying rstrip() function:", rslt_str2)

Output:

Enter some random string = ###btechgeeks****@@@@
Enter some random character = *
The above given first string is : ###btechgeeks****@@@@
The given first string after applying rstrip() function: ###btechgeeks****@@@@
Enter some random string = python-programs_!!!!!&&&&
Enter some random characters = !&
The above given second string is : python-programs_!!!!!&&&&
The given second string after applying rstrip() function: python-programs_

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

Python String rsplit() Method Examples

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

Starting from the right, the rsplit() method splits a string into a list.

If no “max” parameter is specified, this method will return the same value as split().

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

Syntax:

string.rsplit(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 rsplit() 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 rsplit() function:
['good', 'morning', 'this', 'is', 'btechgeeks']

Note:

If you don't give any separator and maxsplit values it takes space,-1 values
respectively by default.

String rsplit() 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 rsplit() method to the given string for the given separator and maxsplit() values which splits a string into a list starting from the right.
  • Store it in another variable.
  • Print the above-given string.
  • Print the above-given string after applying rsplit() 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 rsplit() method to the given string for the given separator and
# maxsplit() which splits a string into a list starting from the right.
# Store it in another variable.
rslt_str1 = gvn_fststr.rsplit(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 rsplit() function.
print("The given first string after applying rsplit() function:")
print(rslt_str1)
# 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_str2 = gvn_scndstr.rsplit()
print("The above given second string is :", gvn_scndstr)
print("The given second string after applying rsplit() function:")
print(rslt_str2)

 Output:

The above given first string is : welcome,to,python,programs
The given first string after applying rsplit() function:
['welcome,to,python', 'programs']
The above given second string is : good morning this is btechgeeks
The given second string after applying rsplit() 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 rsplit() method to the given string for the given separator and maxsplit() values which splits a string into a list starting from the right.
  • Store it in another variable.
  • Print the above-given string.
  • Print the above-given string after applying rsplit() 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 rsplit() method to the given string for the given separator and
# maxsplit() which splits a string into a list starting from the right.
# Store it in another variable.
rslt_str1 = gvn_fststr.rsplit(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 rsplit() function.
print("The given first string after applying rsplit() function:")
print(rslt_str1)
# 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_str2 = gvn_scndstr.rsplit()
print("The above given second string is :", gvn_scndstr)
print("The given second string after applying rsplit() function:")
print(rslt_str2)

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 rsplit() function:
['hello this is', 'btechgeeks']
Enter some random string = hello all goodmorning####
The above given second string is : hello all goodmorning####
The given second string after applying rsplit() function:
['hello', 'all', 'goodmorning####']

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

Python String rpartition() Method Examples

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

The rpartition() method looks for the last occurrence of a string and splits it into a tuple with three elements.

The part preceding the specified string is contained in the first element.

The specified string is contained in the second element.

The part following the string is contained in the third element.

Syntax:

string.rpartition(value)

Parameters

value: This is required. The string to look for.

If the specified value is not found, the rpartition() method returns a tuple containing the following values: 1 – an empty string, 2 – an empty string, 3 – the entire string.

For example:

gvn_str = "welcome to python learning platform"
rslt_tupl = gvn_str.rpartition("java")
print(rslt_tupl)

Output:

('', '', 'welcome to python learning platform')

Examples:

Example1:

Input:

Given string = "welcome to python learning platform, python programs"
Given value = "python"

Output:

The result tuple after applying rpartition() function on the given string : 
('welcome to python learning platform, ', 'python', ' programs')

Example2:

Input:

Given string = "good morning all"
Given value = "all"

Output:

The result tuple after applying rpartition() function on the given string : 
('good morning ', 'all', '')

String rpartition() 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.
  • Give the value as static input and store it in another variable.
  • Apply rpartition() function to the given string to search for the last occurrence of a string and splits it into a tuple with three elements as mentioned above.
  • Store it in another variable.
  • Print the result tuple after applying the rpartition() function on the given string for the given value.
  • Similarly, do the same for the other string and print the result string.
  • The Exit of Program.

Below is the implementation:

# Give the string as static input and store it in a variable.
gvn_str = "welcome to python learning platform, python programs"
# Give the value as static input and store it in another variable.
gvn_valu = "python"
# Apply rpartition() function to the given string to search for the last
# occurrence of a string and splits it into a tuple with three elements
# as mentioned above.
# Store it in another variable.
rslt_tupl = gvn_str.rpartition(gvn_valu)
# Print the result tuple after applying rpartition() function on the given string
# for the given value
print("The result tuple after applying rpartition() function on the given string : ")
print(rslt_tupl)
# Similarly, do the same for the other string and print the result string
gvn_str2 = "good morning all"
gvn_valu2 = "all"
rslt_tupl2 = gvn_str2.rpartition(gvn_valu2)
print("The result tuple after applying rpartition() function on the given second string : ")
print(rslt_tupl2)

Output:

The result tuple after applying rpartition() function on the given string : 
('welcome to python learning platform, ', 'python', ' programs')
The result tuple after applying rpartition() function on the given second string : 
('good morning ', 'all', '')

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.
  • Give the value as user input using the input() function and store it in another variable.
  • Apply rpartition() function to the given string to search for the last occurrence of a string and splits it into a tuple with three elements as mentioned above.
  • Store it in another variable.
  • Print the result tuple after applying the rpartition() function on the given string for the given value.
  • Similarly, do the same for the other string and print the result string.
  • 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 = ")
# Give the value as user input using the input() function and
# store it in another variable.
gvn_valu = input("Enter some random value = ")
# Apply rpartition() function to the given string to search for the last
# occurrence of a string and splits it into a tuple with three elements
# as mentioned above.
# Store it in another variable.
rslt_tupl = gvn_str.rpartition(gvn_valu)
# Print the result tuple after applying rpartition() function on the given string
# for the given value
print("The result tuple after applying rpartition() function on the given string : ")
print(rslt_tupl)
# Similarly, do the same for the other string and print the result string
gvn_str2 = input("Enter some random string = ")
gvn_valu2 = input("Enter some random value = ")
rslt_tupl2 = gvn_str2.rpartition(gvn_valu2)
print("The result tuple after applying rpartition() function on the given second string : ")
print(rslt_tupl2)

Output:

Enter some random string = hello this is btechgeeks hello
Enter some random value = hello
The result tuple after applying rpartition() function on the given string : 
('hello this is btechgeeks ', 'hello', '')
Enter some random string = good morning all this is python programming
Enter some random value = good
The result tuple after applying rpartition() function on the given second string : 
('', 'good', ' morning all this is python programming')

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

Python String rjust() Method Examples

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

The rjust() method will right-align the string, using a specified character as the fill character (space is the default).

Syntax:

string.rjust(Length, character)

Parameters

Length: This is required. It is the length of the string returned.

character: This is optional. Fills the character in the missing space( left of the string). The default value is ” ” (space).

Examples:

Example1:

Input:

Given first string = "python"
Given length = 15 
Given character = '-'

Output:

The above given first string is : python
The given first string after applying rjust() function: ---------python

Example2:

Input:

Given second string = "GOODMORNING"
Given length = 17
Given character = '#'

Output:

The above given second string is : GOODMORNING
The given second string after applying rjust() function: ######GOODMORNING

String rjust() 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 length as static input and store it in another variable.
  • Give the character as static input and store it in another variable.
  • Apply rjust() method to the given string for the given length and the character which will right-align the string, using a specified character as the fill character (space is the default).
  • Print the above-given string.
  • Print the above-given string after applying the rjust() 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 = "python"
# Give the length as static input and store it in another variable.
gvn_lengt = 15
# Give the character as static input and store it in another variable.
gvn_chactr = '-'
# Apply rjust() method to the given string for the given length and the
# character which will right align the string, using a specified character
# as the fill character (space is the default).
# Store it in another variable.
rslt_str1 = gvn_fststr.rjust(gvn_lengt, gvn_chactr)
# Print the above given string
print("The above given first string is :", gvn_fststr)
# Print the above given string after applying rjust() function.
print("The given first string after applying rjust() function:", rslt_str1)
# Similarly do the same for other string and print the result string.
gvn_scndstr = "GOODMORNING"
gvn_lengt2 = 17
gvn_chactr2 = '#'
rslt_str2 = gvn_scndstr.rjust(gvn_lengt2, gvn_chactr2)
print("The above given second string is :", gvn_scndstr)
print("The given second string after applying rjust() function:", rslt_str2)

Output:

The above given first string is : python
The given first string after applying rjust() function: ---------python
The above given second string is : GOODMORNING
The given second string after applying rjust() function: ######GOODMORNING

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 length as user input using the int(input()) function and store it in another variable.
  • Give the character as user input using the input() function and store it in another variable.
  • Apply rjust() method to the given string for the given length and the character which will right-align the string, using a specified character as the fill character (space is the default).
  • Print the above-given string.
  • Print the above-given string after applying the rjust() 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 = ")
# Give the length as user input using the int(input()) function and store it in another variable.
gvn_lengt = int(input("Enter some random number = "))
# Give the character as user input using the input() function and store it in another variable.
gvn_chactr = input("Enter some random charcter = ")
# Apply rjust() method to the given string for the given length and the
# character which will right align the string, using a specified character
# as the fill character (space is the default).
# Store it in another variable.
rslt_str1 = gvn_fststr.rjust(gvn_lengt, gvn_chactr)
# Print the above given string
print("The above given first string is :", gvn_fststr)
# Print the above given string after applying rjust() function.
print("The given first string after applying rjust() function:", rslt_str1)
# Similarly do the same for other string and print the result string.
gvn_scndstr = input("Enter some random string = ")
gvn_lengt2 = int(input("Enter some random number = "))
gvn_chactr2 = input("Enter some random charcter = ")
rslt_str2 = gvn_scndstr.rjust(gvn_lengt2, gvn_chactr2)
print("The above given second string is :", gvn_scndstr)
print("The given second string after applying rjust() function:", rslt_str2)

Output:

Enter some random string = hello
Enter some random number = 10
Enter some random charcter = %
The above given first string is : hello
The given first string after applying rjust() function: %%%%%hello
Enter some random string = btechgeeks
Enter some random number = 20
Enter some random charcter = $
The above given second string is : btechgeeks
The given second string after applying rjust() function: $$$$$$$$$$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.

 

 

Python String rjust() Method Examples Read More »

Python String rindex() Method Examples

In the previous article, we have discussed Python String rfind() Method Examples
rindex() Function in Python:

The rindex() method returns the location of the last occurrence of the specified value.

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

The rindex() method is nearly identical to the rfind() method.

Note: The only difference is rindex() method throws an exception if the value is not found whereas rfind() returns a value -1.

Syntax:

string.rindex(value, start, end)

Parameters

value: This is required. The value to look for

start: This is optional. Where to begin your search. The default value is 0.

end: This is optional. Where to put an end to the search. The default value is to the end of the string.

Examples:

Example1:

Input:

Given string =  "welcome to python learning platform"
Given value = "p"

Output:

The result after applying rindex() function on the given string =  27

Example2:

Input:

Given string = "good morning btechgeeks"
Given value = 'e'
Given start index = 3
Given end index = 16

Output:

The result after applying rindex() function on the given string =  15

 String rindex() 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.
  • Give the value as static input and store it in another variable.
  • Apply rindex() function to the given string for the given value that returns the location of the last occurrence of the specified value.
  • Store it in another variable.
  • Print the result after applying rindex() function on the given string for the given value.
  • Similarly, do the same for the other values by giving the start and end positions as static input 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_str = "welcome to python learning platform"
# Give the value as static input and store it in another variable.
gvn_valu = "p"
# Apply rindex() function to the given string for the given value that
# returns the location of the last occurrence of the specified value.
# Store it in another variable.
rslt = gvn_str.rindex(gvn_valu)
# Print the result after applying rindex() function on the given string
# for the given value.
print("The result after applying rindex() function on the given string = ", rslt)
# Similarly, do the same for the other values and print the result.
rslt2 = gvn_str.rindex('o', 1, 8)
rslt3 = gvn_str.rindex('m')
print("The result after applying rindex() function on the given string = ", rslt2)
print("The result after applying rindex() function on the given string = ", rslt3)

Output:

The result after applying rindex() function on the given string =  27
The result after applying rindex() function on the given string =  4
The result after applying rindex() function on the given string =  34

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.
  • Give the value as user input using the input() function and store it in another variable.
  • Apply rindex() function to the given string for the given value that returns the location of the last occurrence of the specified value.
  • Store it in another variable.
  • Print the result after applying rindex() function on the given string for the given value.
  • Similarly, do the same for the other value by giving the start and end positions as user input using the int(input()) function 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_str = input("Enter some random string = ")
# Give the value as user input using the input() function and 
# store it in another variable.
gvn_valu = input("Enter some random value = ")
# Apply rindex() function to the given string for the given value that
# returns the location of the last occurrence of the specified value.
# Store it in another variable.
rslt = gvn_str.rindex(gvn_valu)
# Print the result after applying rindex() function on the given string
# for the given value.
print("The result after applying rindex() function on the given string = ", rslt)
# Similarly, do the same for the other value by giving the start and end positions
# as user input using the int(input()) function and print the result.
gvn_valu2 = input("Enter some random value = ")
gvn_strtindx = int(input("Enter some random number = "))
gvn_endindx = int(input("Enter some random number = "))
rslt2 = gvn_str.rindex(gvn_valu2, gvn_strtindx, gvn_endindx)
print("The result after applying rindex() function on the given string = ", rslt2)

Output:

Enter some random string = good morning btechgeeks
Enter some random value = o
The result after applying rindex() function on the given string = 6
Enter some random value = e
Enter some random number = 3
Enter some random number = 16
The result after applying rindex() function on the given string = 15

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

Python String rfind() Method Examples

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

The rfind() method gives the position of the last occurrence of the specified value.

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

The rfind() method is nearly identical to the rindex() method.

Syntax:

string.rfind(value, start, end)

Parameters

value: This is required. The value to look for

start: This is optional. Where to begin your search. The default value is 0.

end: This is optional. Where to put an end to the search. The default value is to the end of the string.

Examples:

Example1:

Input:

Given string =  "welcome to python learning platform"
Given value = "p"

Output:

The result after applying rfind() function on the given string =  27

Example2:

Input:

Given string = "welcome to python learning platform"
Given value = "o"
Given start index = 1
Given end index = 8

Output:

The result after applying rfind() function on the given string =  4

String rfind() 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.
  • Give the value as static input and store it in another variable.
  • Apply rfind() function to the given string for the given value that gives the last occurrence of the specified value.
  • Store it in another variable.
  • Print the result after applying rfind() function on the given string for the given value.
  • Similarly, do the same for the other values 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_str = "welcome to python learning platform"
# Give the value as static input and store it in another variable.
gvn_valu = "p"
# Apply rfind() function to the given string for the given value that gives the last
# occurrence of the specified value.
# Store it in another variable.
rslt = gvn_str.rfind(gvn_valu)
# Print the result after applying rfind() function on the given string
# for the given value.
print("The result after applying rfind() function on the given string = ", rslt)
# Similarly, do the same for the other values and print the result.
rslt2 = gvn_str.rfind('o', 1, 8)
rslt3 = gvn_str.rfind('b')
print("The result after applying rfind() function on the given string = ", rslt2)
print("The result after applying rfind() function on the given string = ", rslt3)

Output:

The result after applying rfind() function on the given string =  27
The result after applying rfind() function on the given string =  4
The result after applying rfind() function on the given string =  -1

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.
  • Give the value as user input using the input() function and store it in another variable.
  • Apply rfind() function to the given string for the given value that gives the last occurrence of the specified value.
  • Store it in another variable.
  • Print the result after applying rfind() function on the given string for the given value.
  • Similarly, do the same for the other value by giving the start and end positions as user input using the int(input()) function 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_str = input("Enter some random string = ")
# Give the value as user input using the input() function and 
# store it in another variable.
gvn_valu = input("Enter some random value = ")
# Apply rfind() function to the given string for the given value that gives the last
# occurrence of the specified value.
# Store it in another variable.
rslt = gvn_str.rfind(gvn_valu)
# Print the result after applying rfind() function on the given string
# for the given value.
print("The result after applying rfind() function on the given string = ", rslt)
# Similarly, do the same for the other value by giving the start and end positions
# as user input using the int(input()) function and print the result.
gvn_valu2 = input("Enter some random value = ")
gvn_strtindx = int(input("Enter some random number = "))
gvn_endindx = int(input("Enter some random number = "))
rslt2 = gvn_str.rfind(gvn_valu2, gvn_strtindx, gvn_endindx)
print("The result after applying rfind() function on the given string = ", rslt2)

Output:

Enter some random string = hello this is btechgeeks
Enter some random value = e
The result after applying rfind() function on the given string = 21
Enter some random value = t
Enter some random number = 1
Enter some random number = 20
The result after applying rfind() function on the given string = 15

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

Python String partition() Method Examples

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

The partition() method looks for a specified string and splits it into a tuple with three elements.

The part preceding the specified string is contained in the first element.

The specified string is contained in the second element.

The part following the string is contained in the third element.

Note: partition() method will look for the first occurrence of the specified string.

Syntax:

string.partition(value)

Parameters

value: This is required. The string to look for.

If the specified value is not found, the partition() method returns a tuple containing the following values: 1 – the entire string, 2 – an empty string, 3 – an empty string.

For example:

gvn_str = "welcome to python learning platform"
rslt_tupl = gvn_str.partition("java")
print(rslt_tupl)

Output:

('welcome to python learning platform', '', '')

Examples:

Example1:

Input:

Given string = "welcome to python learning platform"
Given value = "python"

Output:

The result tuple after applying partition() function on the given string : 
('welcome to ', 'python', ' learning platform')

Example2:

Input:

Given string = "good morning all"
Given value = "all"

Output:

The result tuple after applying partition() function on the given string : 
('good morning ', 'all', '')

Program for partition() Function in Python

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

Approach:

  • Give the string as static input and store it in a variable.
  • Give the value as static input and store it in another variable.
  • Apply partition() function to the given string for the given value to split the given string into a tuple with three elements as mentioned above.
  • Store it in another variable.
  • Print the result tuple after applying the partition() function on the given string for the given value.
  • Similarly, do the same for the other string and print the result string.
  • The Exit of Program.

Below is the implementation:

# Give the string as static input and store it in a variable.
gvn_str = "welcome to python learning platform"
# Give the value as static input and store it in another variable.
gvn_valu = "python"
# Apply partition() function to the given string for the given value to split
# the given string into a tuple with three elements as mentioned above.
# Store it in another variable.
rslt_tupl = gvn_str.partition(gvn_valu)
# Print the result tuple after applying partition() function on the given string
# for the given value
print("The result tuple after applying partition() function on the given string : ")
print(rslt_tupl)
# Similarly, do the same for the other string and print the result string
gvn_str2 = "good morning all"
gvn_valu2 = "all"
rslt_tupl2 = gvn_str2.partition(gvn_valu2)
print("The result tuple after applying partition() function on the given second string : ")
print(rslt_tupl2)

Output:

The result tuple after applying partition() function on the given string : 
('welcome to ', 'python', ' learning platform')
The result tuple after applying partition() function on the given second string : 
('good morning ', 'all', '')

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.
  • Give the value as user input using the input() function and store it in another variable.
  • Apply partition() function to the given string for the given value to split the given string into a tuple with three elements as mentioned above.
  • Store it in another variable.
  • Print the result tuple after applying the partition() function on the given string for the given value.
  • Similarly, do the same for the other string and print the result string.
  • 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 = ")
# Give the value as user input using the input() function and
# store it in another variable.
gvn_valu = input("Enter some random value = ")
# Apply partition() function to the given string for the given value to split
# the given string into a tuple with three elements as mentioned above.
# Store it in another variable.
rslt_tupl = gvn_str.partition(gvn_valu)
# Print the result tuple after applying partition() function on the given string
# for the given value
print("The result tuple after applying partition() function on the given string : ")
print(rslt_tupl)
# Similarly, do the same for the other string and print the result string
gvn_str2 = input("Enter some random string = ")
gvn_valu2 = input("Enter some random value = ")
rslt_tupl2 = gvn_str2.partition(gvn_valu2)
print("The result tuple after applying partition() function on the given second string : ")
print(rslt_tupl2)

Output:

Enter some random string = this is python coding platform
Enter some random value = coding
The result tuple after applying partition() function on the given string : 
('this is python ', 'coding', ' platform')
Enter some random string = hello all 12345
Enter some random value = 12345
The result tuple after applying partition() function on the given second string : 
('hello all ', '12345', '')

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

Python String lstrip() Method Examples

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

Any leading characters are removed using the lstrip() method (space is the default leading character to remove)

Syntax:

string.lstrip(characters)

Parameters

characters: This parameter is optional, and omitting it causes the lstrip function to treat white spaces as the default parameter. In Python, specify the Characters to strip from the left-hand side of a string to change the default white space.

Examples:

Example1:

Input:

Given first String =  "####python###"
Given character = '#'

Output:

The above given first string is : ####python###
The given first string after applying lstrip() function: python###

Example2:

Input:

Given second String = "@@@@@###_GOODMORNING_22222"
Given characters = "@,#"

Output:

The above given second string is : @@@@@###_GOODMORNING_22222
The given second string after applying lstrip() function: _GOODMORNING_22222

Program for lstrip() 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.
  • Give the character as static input and store it in another variable.
  • Apply lstrip() method to the given string for the given character in which any leading characters are removed using the lstrip() method (space is the default leading character to remove).
  • Store it in another variable.
  • Print the above-given string.
  • Print the above-given string after applying the lstrip() 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 = "####python###"
# Give the character as static input and store it in another variable.
gvn_chactr = '#'
# Apply lstrip() method to the given string for the given character in which
# any leading characters are removed using the lstrip() method
# (space is the default leading character to remove).
# Store it in another variable.
rslt_str1 = gvn_fststr.lstrip(gvn_chactr)
# Print the above given string
print("The above given first string is :", gvn_fststr)
# Print the above given string after applying lstrip() function.
print("The given first string after applying lstrip() function:", rslt_str1)
# Similarly do the same for other string and print the result string.
gvn_scndstr = "@@@@@###_GOODMORNING_22222"
rslt_str2 = gvn_scndstr.lstrip()
print("The above given second string is :", gvn_scndstr)
print("The given second string after applying lstrip() function:", rslt_str2)

Output:

The above given first string is : ####python###
The given first string after applying lstrip() function: python###
The above given second string is : @@@@@###_GOODMORNING_22222
The given second string after applying lstrip() function: @@@@@###_GOODMORNING_22222

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 character as user input using the input() function and store it in another variable.
  • Apply lstrip() method to the given string for the given character in which any leading characters are removed using the lstrip() method (space is the default leading character to remove).
  • Store it in another variable.
  • Print the above-given string.
  • Print the above-given string after applying the lstrip() 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 = ")
# Give the character as user input using the input() function and store it in another variable.
gvn_chactr = input("Enter some random character = ")
# Apply lstrip() method to the given string for the given character in which
# any leading characters are removed using the lstrip() method
# (space is the default leading character to remove).
# Store it in another variable.
rslt_str1 = gvn_fststr.lstrip(gvn_chactr)
# Print the above given string
print("The above given first string is :", gvn_fststr)
# Print the above given string after applying lstrip() function.
print("The given first string after applying lstrip() function:", rslt_str1)
# Similarly do the same for other string and print the result string.
gvn_scndstr = input("Enter some random string = ")
gvn_chactr2 = input("Enter some random character = ")
rslt_str2 = gvn_scndstr.lstrip(gvn_chactr2)
print("The above given second string is :", gvn_scndstr)
print("The given second string after applying lstrip() function:", rslt_str2)

Output:

Enter some random string = hello all
Enter some random character = ^
The above given first string is : hello all
The given first string after applying lstrip() function: hello all
Enter some random string = 555555python_platform_4545
Enter some random character = 5
The above given second string is : 555555python_platform_4545
The given second string after applying lstrip() function: python_platform_4545

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

Python String ljust() Method Examples

In the previous article, we have discussed Python Program for String join()
ljust() Function in Python:

The ljust() method will left align the string, using a specified character as the fill character (space is the default).

Syntax:

string.ljust(Length, character)

Parameters

Length: This is required. It is the length of the string returned.

character: This is optional. Fills the character in the missing space( right of the string). The default value is ” ” (space).

Examples:

Example1:

Input:

Given first string = "python"
Given length = 15 
Given character = '-'

Output:

The above given first string is : python
The given first string after applying ljust() function: python---------

Note: If you don’t give any character then it takes space by default. 

Example2:

Input:

Given second string = "GOODMORNING"
Given length = 17
Given character = '#'

Output:

The above given second string is : GOODMORNING
The given second string after applying ljust() function: GOODMORNING######

Program for ljust() 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.
  • Give the length as static input and store it in another variable.
  • Give the character as static input and store it in another variable.
  • Apply ljust() method to the given string for the given length and the character which will left align the string, using a specified character as the fill character (space is the default).
  • Print the above-given string.
  • Print the above-given string after applying the ljust() 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 = "python"
# Give the length as static input and store it in another variable.
gvn_lengt = 15
# Give the character as static input and store it in another variable.
gvn_chactr = '-'
# Apply ljust() method to the given string for the given length and the
# character which will left align the string, using a specified character
# as the fill character (space is the default).
# Store it in another variable.
rslt_str1 = gvn_fststr.ljust(gvn_lengt, gvn_chactr)
# Print the above given string
print("The above given first string is :", gvn_fststr)
# Print the above given string after applying ljust() function.
print("The given first string after applying ljust() function:", rslt_str1)
# Similarly do the same for other string and print the result string.
gvn_scndstr = "GOODMORNING"
gvn_lengt2 = 17
gvn_chactr2 = '#'
rslt_str2 = gvn_scndstr.ljust(gvn_lengt2, gvn_chactr2)
print("The above given second string is :", gvn_scndstr)
print("The given second string after applying ljust() function:", rslt_str2)

Output:

The above given first string is : python
The given first string after applying ljust() function: python---------
The above given second string is : GOODMORNING
The given second string after applying ljust() function: GOODMORNING######

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 length as user input using the int(input()) function and store it in another variable.
  • Give the character as user input using the input() function and store it in another variable.
  • Apply ljust() method to the given string for the given length and the character which will left align the string, using a specified character as the fill character (space is the default).
  • Print the above-given string.
  • Print the above-given string after applying the ljust() 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 = ")
# Give the length as user input using the int(input()) function and store it in another variable.
gvn_lengt = int(input("Enter some random number = "))
# Give the character as user input using the input() function and store it in another variable.
gvn_chactr = input("Enter some random charcter = ")
# Apply ljust() method to the given string for the given length and the
# character which will left align the string, using a specified character
# as the fill character (space is the default).
# Store it in another variable.
rslt_str1 = gvn_fststr.ljust(gvn_lengt, gvn_chactr)
# Print the above given string
print("The above given first string is :", gvn_fststr)
# Print the above given string after applying ljust() function.
print("The given first string after applying ljust() function:", rslt_str1)
# Similarly do the same for other string and print the result string.
gvn_scndstr = input("Enter some random string = ")
gvn_lengt2 = int(input("Enter some random number = "))
gvn_chactr2 = input("Enter some random charcter = ")
rslt_str2 = gvn_scndstr.ljust(gvn_lengt2, gvn_chactr2)
print("The above given second string is :", gvn_scndstr)
print("The given second string after applying ljust() function:", rslt_str2)

Output:

Enter some random string = hello
Enter some random number = 10
Enter some random charcter = %
The above given first string is : hello
The given first string after applying ljust() function: hello%%%%%
Enter some random string = goodmorning
Enter some random number = 25
Enter some random charcter = $
The above given second string is : goodmorning
The given second string after applying ljust() function: goodmorning$$$$$$$$$$$$$$

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

Python String join() Method Examples

In the previous article, we have discussed Python Program for isdigit() Function
String join() Function in Python:

The join() method joins all items in an iterable into a single string.

As the separator, a string must be specified.

Syntax:

string.join(iterable)

Parameters:

iterable: This is required. Any iterable object whose returned values are all strings.

Examples:

Example1:

Input:

Given list = ['welcome', 'to', 'python', 'programs']
Given separator = '@'

Output:

The given list items after applying join() function :  welcome@to@python@programs

Example2:

Input:

Given tuple = ('hello', 'all', 'good', 'morning')
Given separator = '-'

Output:

The given tuple items after applying join() function :  hello-all-good-morning

Program for String join() in Python

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

Approach:

  • Give the list as static input and store it in a variable.
  • Give the tuple as static input and store it in another variable.
  • Give the set as static input and store it in another variable.
  • Give the first separator as static input and store it in another variable.
  • Give the second separator as static input and store it in another variable.
  • Give the third separator as static input and store it in another variable.
  • Apply join() function to the given list to joins all items in a list into a single string for the given separator1.
  •  Store it in another variable.
  • Apply join() function to the given tuple to joins all items in a tuple into a single string for the given separator2.
  • Store it in another variable.
  • Print the result string after joining the given list items using the join() function.
  • Print the result string after joining the given tuple items using the join() function.
  • Similarly, do the same for the given set with the given third separator and print the result string.
  • The Exit of Program.

Below is the implementation:

# Give the list as static input and store it in a variable.
gvn_lst = ['welcome', 'to', 'python', 'programs']
# Give the tuple as static input and store it in another variable.
gvn_tupl = ('hello', 'all', 'good', 'morning')
# Give the set as static input and store it in another variable.
gvn_set = ('This', 'is', 'python', 'learning', 'platform')
# Give the first separator as static input and store it in another variable.
gvn_sepatr1 = '@'
# Give the second separator as static input and store it in another variable.
gvn_sepatr2 = '-'
# Give the third separator as static input and store it in another variable.
gvn_sepatr3 = '#'
# Apply join() function to the given list to joins all items in a list into a
# single string for the given separator1.
# Store it in another variable.
rsltstrng1 = gvn_sepatr1.join(gvn_lst)
# Apply join() function to the given tuple to joins all items in a tuple into a
# single string for the given separator2.
# Store it in another variable.
rsltstrng2 = gvn_sepatr2.join(gvn_tupl)
print("The given list items after applying join() function : ", rsltstrng1)
print("The given tuple items after applying join() function : ", rsltstrng2)
# Similarly, do the same for the given set with given third separator and
# print the result string
rsltstrng3 = gvn_sepatr3.join(gvn_set)
print("The given set items after applying join() function : ", rsltstrng3)

Output:

The given list items after applying join() function :  welcome@to@python@programs
The given tuple items after applying join() function :  hello-all-good-morning
The given set items after applying join() function :  This#is#python#learning#platform

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

Approach:

  • Give the list as user input using list(), input(), and split() functions.
  • Store it in a variable.
  • Give the tuple as user input using list(), input(), and split() functions.
  • Store it in another variable.
  • Give the set as user input using list(), input(), and split() functions.
  • Store it in another variable.
  • Give the first separator as user input using the input() function and store it in another variable.
  • Give the second separator as user input using the input() function and store it in another variable.
  • Give the third separator as user input using the input() function and store it in another variable.
  • Apply join() function to the given list to joins all items in a list into a single string for the given separator1.
  •  Store it in another variable.
  • Apply join() function to the given tuple to joins all items in a tuple into a single string for the given separator2.
  • Store it in another variable.
  • Print the result string after joining the given list items using the join() function.
  • Print the result string after joining the given tuple items using the join() function.
  • Similarly, do the same for the given set with the given third separator and print the result string.
  • The Exit of Program.

Below is the implementation:

# Give the list as user input using list(),input() and split() functions.
# Store it in a variable.
gvn_lst = list(input(
    'Enter some random List Elements separated by spaces = ').split())
# Give the tuple as user input using list(), input(),and split() functions.
# Store it in another variable.
gvn_tupl = tuple(input(
    'Enter some random Tuple Elements separated by spaces = ').split())
# Give the set as user input using list(), input(),and split() functions.
# Store it in another variable.
gvn_set = set(input(
    'Enter some random Set Elements separated by spaces = ').split())
# Give the first separator as user input using the input() function and 
# store it in another variable.
gvn_sepatr1 = input("Enter a random element = ")
# Give the second separator as user input using the input() function and 
# store it in another variable.
gvn_sepatr2 = input("Enter a random element = ")
# Give the third separator as user input using the input() function and 
# store it in another variable.
gvn_sepatr3 = input("Enter a random element = ")
# Apply join() function to the given list to joins all items in a list into a
# single string for the given separator1.
# Store it in another variable.
rsltstrng1 = gvn_sepatr1.join(gvn_lst)
# Apply join() function to the given tuple to joins all items in a tuple into a
# single string for the given separator2.
# Store it in another variable.
rsltstrng2 = gvn_sepatr2.join(gvn_tupl)
# Print the result string after joining the given list items using the join()
# function
print("The given list items after applying join() function : ", rsltstrng1)
# Print the result string after joining the given tuple items using the join()
# function
print("The given tuple items after applying join() function : ", rsltstrng2)
# Similarly, do the same for the given set with given third separator and
# print the result string
rsltstrng3 = gvn_sepatr3.join(gvn_set)
print("The given set items after applying join() function : ", rsltstrng3)

Output:

Enter some random List Elements separated by spaces = hello this is python learning platform
Enter some random Tuple Elements separated by spaces = p r q s t
Enter some random Set Elements separated by spaces = good morning all
Enter a random element = &
Enter a random element = *
Enter a random element = !
The given list items after applying join() function : hello&this&is&python&learning&platform
The given tuple items after applying join() function : p*r*q*s*t
The given set items after applying join() function : good!morning!all

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