Convert Percentage String To Decimal Like 30% To 0.3 In Python

How do you convert a string representing a percentage number into an actual number that can be used to perform calculations? To convert a string represented as a percentage number into an actual decimal number in Python use the built-in float() function after removing the percent symbol from the string and any other characters that prevent numeric conversion. Without purging from the original string the percent symbol you will get a ValueError as demonstrated below in the Python REPL: ...

February 10, 2022 · 4 min · 658 words · Ryan Sheehy

3 Ways To Check If String Can Convert To Integer In Python

How do you know if a string will convert to an integer in Python? There are 3 ways to check if a string will convert to an integer in Python and these methods are: use a try-catch on the int(string) operation or perform an operation on the string to remove all integers and see if anything is left – use either the regex library or the string.replace() method. Let’s look at each approach in a little more detail and use an example or two. ...

February 7, 2022 · 4 min · 829 words · Ryan Sheehy

How To Format Text Like 1st Jan 22 To A Date In Google Sheets

How do you format a cell that contains text like 1st Jan 2022 into a cell that Google Sheets can recognize as a date cell? There are certain types of date formats that can prevent the automatic import of data into Google Sheets into dates. One recent data type I had to deal with was where the day field was an abbreviated ordinal number (i.e. 1st, 2nd, 3rd, 4th… etc) and the month was an abbreviated month (eg. Jan, Feb, Mar… etc) and even the year was abbreviated too (eg. 2022 = 22, 2023 = 23, etc). ...

February 5, 2022 · 13 min · 2769 words · Ryan Sheehy

How To Capitalize First Letter Of Every Word In Python (One-Liner)

How do you capitalize the first letter of each word in a string using Python? To capitalize the first letter of every word in Python using one line of code enter the following: " ".join([x.capitalize() for x in my_string.split()]). Here is an example demonstrating how the code works in the Python REPL: 1 2 3 >>> my_string = "How long is a piece of string?" >>> " ".join([x.capitalize() for x in my_string.split()]) 'How Long Is A Piece Of String?' As you can see from the above example the output produces a string with each character capitalized. ...

February 2, 2022 · 8 min · 1684 words · Ryan Sheehy

How To Find The Length Of A String In Python Using Len

How do you find the length of the string in Python without importing any libraries? To find the length of a string in Python use the built-in len() function, which takes one parameter that can be of data type string, bytes, tuple, list, range, or a collection (such as a dictionary, set, or frozen set). The len() function, when used on a string, will return the number of characters in that string. ...

February 2, 2022 · 6 min · 1070 words · Ryan Sheehy

Does Python Have Ternary Operator?

Does Python have a ternary operator? If so, how does it work? A ternary operator, otherwise known as a conditional expression, is a simple if-else statement that is performed on one line of code to evaluate a condition. The value entered before the if clause is the True value returned, whereas the value after the else clause is the False value returned. The expression between the if and the else is the conditional clause that determines what value is returned. ...

January 29, 2022 · 5 min · 997 words · Ryan Sheehy

What Is The Difference Between AND And OR In Python?

What is the difference between using the logical operators and and or in Python? When using the and logical operator in Python all conditions in the statement need to be True for the entire expression to evaluate to True. If one condition evaluates to False then the entire expression evaluates to False. When using the or logical operator only one condition in the entire expression needs to evaluate to True for the entire expression to evaluate to True. If all conditions are False then the expression evaluates to False. ...

January 23, 2022 · 5 min · 982 words · Ryan Sheehy

Python Walrus Operator In 3 Minutes

What does := mean and do in Python? Since Python version 3.8 the new assignment expression, also known as the walrus operator, enables developers the ability to assign a variable from the result of a function or operation and then have that assigned variable reused elsewhere in the code. One recent example in my code on this site was halving a string using Python. With this example I had the option of repeating my code multiple times in the simple one-liner I had created. Here is a copy of that code, and how it would have been handled without using the walrus operator: ...

January 21, 2022 · 2 min · 402 words · Ryan Sheehy

How To Split A String In Half In Python: 1 Line Of Code

How do you split a string into two equal halves using Python? To tackle this exercise you need to know how you’re going to operate on odd numbered string lengths. For example, how would you like your code to operate on the string halve. As it has 5 characters you’re going to have 5 possible solutions: hal and ve OR ha and lve ha, l and ve halve OR None The first solution just placed the middle character to either side giving the extra character to the first half or second half. ...

January 21, 2022 · 4 min · 755 words · Ryan Sheehy

How To Sort By Date In Google Sheets And What To Do When It Doesn't Work

How do you sort a range containing dates in Google Sheets? Sorting a data set in columns or rows containing date data in Google Sheets can easily be done by highlighting the range and then clicking on Data > Sort Range > Advanced range sorting options, as shown below: If the Sort Range option is greyed out it means you haven’t selected a range. Check you have a range highlighted as shown above where I’ve selected cells A1 to B4. ...

January 19, 2022 · 4 min · 820 words · Ryan Sheehy