Check If a String Starts and Ends with the Same Character in Python

AmitDiwan
Updated on 20-Sep-2021 10:39:53

1K+ Views

When it is required to check if a string begins and ends with the same character or not, regular expression can be used. A method can be defined that uses the ‘search’ function to see if a string begins and ends with a specific character.ExampleBelow is a demonstration of the sameimport re regex_expression = r'^[a-z]$|^([a-z]).*\1$' def check_string(my_string): if(re.search(regex_expression, my_string)): print("The given string starts and ends with the same character") else: print("The given string doesnot start and end with the ... Read More

Make a Rug Plot in Matplotlib

Rishikesh Kumar Rishi
Updated on 20-Sep-2021 10:37:03

844 Views

Rug plots are used to visualize the distribution of data. It is a plot of data for a single variable, displayed as marks along an axis. To make a rug plot in Matplotlib, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Create x data points using numpy.Add representation of a kernel-density estimate using Gaussian kernels, kde1 and kde2.Create a new figure or activate an existing figure using figure() method.Add an 'ax1' to the figure as part of a subplot arrangement.Make a rug plot with marker_size=20.Plot x_eval, kde1(x_eval) and kde2(x_eval) data ... Read More

Check If a String Starts With a Substring Using Regex in Python

AmitDiwan
Updated on 20-Sep-2021 10:37:01

492 Views

When it is required to check if a string starts with a specific substring or not, with the help of regular expression, a method is defined that iterates through the string and uses the ‘search’ method to check if a string begins with a specific substring or not.ExampleBelow is a demonstration of the sameimport re def check_string(my_string, sub_string) : if (sub_string in my_string): concat_string = "^" + sub_string result = re.search(concat_string, my_string) if result : ... Read More

Python Regex to Find Sequences of One Upper Case Letter Followed by Lower Case Letters

AmitDiwan
Updated on 20-Sep-2021 10:34:15

891 Views

When it is required to find sequences of an upper case letter followed by lower case using regular expression, a method named ‘match_string’ is defined that uses the ‘search’ method to match a regular expression. Outside the method, the string is defined, and the method is called on it by passing the string.ExampleBelow is a demonstration of the sameimport re def match_string(my_string):    pattern = '[A-Z]+[a-z]+$'    if re.search(pattern, my_string):       return('The string meets the required condition ')    else:       return('The string doesnot meet the required condition ') print("The string is ... Read More

Remove All Characters Except Letters and Numbers in Python

AmitDiwan
Updated on 20-Sep-2021 10:23:57

962 Views

When it is required to remove all characters except for letters and numbers, regular expressions are used. A regular expression is defined, and the string is subjected to follow this expression.ExampleBelow is a demonstration of the sameimport re my_string = "python123:, .@! abc" print ("The string is : ") print(my_string) result = re.sub('[\W_]+', '', my_string) print ("The expected string is :") print(result)OutputThe string is : python123:, .@! abc The expected string is : python123abcExplanationThe required packages are imported.A string is defined and is displayed on the console.A regular expression is defined, and the string is subjected ... Read More

Find Least Frequent Character in a String using Python

AmitDiwan
Updated on 20-Sep-2021 10:20:05

755 Views

When it is required to find the least frequent character in a string, ‘Counter’ is used to get the count of letters. The ‘min’ method is used to get the minimum of values in the string, i.e every letter’s count is stored along with the letter. The minimum is obtained.ExampleBelow is a demonstration of the samefrom collections import Counter my_str = "highland how" print ("The string is : ") print(my_str) my_result = Counter(my_str) my_result = min(my_result, key = my_result.get) print ("The minimum of all characters in the string is : ") print(my_result)OutputThe string is : highland ... Read More

Fill Rainbow Color Under a Curve in Python Matplotlib

Rishikesh Kumar Rishi
Updated on 20-Sep-2021 09:54:32

750 Views

To fill rainbow color under a curve in Python Matplotlib, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Create a user-defined method, plot_rainbow_under_curve(), that could have a list of 7 rainbow colors and create a set of data points "x" using numpy.Iterate in the range of 0 to 7 and plot the curve and fill the area between that curve.To display the figure, use show() method.Exampleimport numpy as np import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True def plot_rainbow_under_curve(): rainbow_colors = ['violet', 'indigo', ... Read More

Draw Axis Lines Inside a Plot in Matplotlib

Rishikesh Kumar Rishi
Updated on 20-Sep-2021 09:38:32

595 Views

To draw axis lines inside a plot in Matplotlib, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Create a new figure or activate an existing figure.Create x data points using numpy.Add an 'ax' to the figure as part of a subplot arrangement.Plot x and x**x data points using plot() method.Set the left and bottom positions at 0, whereas color of the right and top spines none.To display the figure, use show() method.Exampleimport numpy as np import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True fig = ... Read More

Set Same Scale for Subplots in Python Using Matplotlib

Rishikesh Kumar Rishi
Updated on 20-Sep-2021 09:32:07

4K+ Views

To set the same scale for subplot in Python using Matplotlib, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Create a new figure or activate an existing figure.Add an 'ax1' to the figure as part of a subplot arrangement with nrows=2, ncols=1 and index=1.Add another axis 'ax2' to the figure as part of a subplot arrangement with nrows=2, ncols=1 and index=2, with shared X-axis (to set same scale for subplots)Create "t" data points to plot sine and cosine curves on axes ax1 and ax2.To display the figure, use show() method.Exampleimport ... Read More

Conditional Removal of Labels in Matplotlib Pie Chart

Rishikesh Kumar Rishi
Updated on 20-Sep-2021 09:29:16

2K+ Views

To remove labels from a Matplotlib pie chart based on a condition, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Create a Pandas dataframe of wwo-dimensional, size-mutable, potentially heterogeneous tabular data.Plot a pie chart, using pie() method with conditional removal of labels, such that if %age value is greater than 25, then only keep labels, otherwise remove them.To display the figure, use show() method.Exampleimport pandas as pd from matplotlib import pyplot as plt # Set the figure size plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True # Create a ... Read More

Advertisements