Python String

56 Python One-Liners to Impress Your Friends

This is a running document in which I’ll answer all questions regarding the single line of Python code. It’s based on my interactive collection here but without the slow videos and embedded code shells. Let’s get started! Python One Line If Else You can use a simple if statement in a single line of code. …

56 Python One-Liners to Impress Your Friends Read More »

Python Unicode Encode Error

Summary: The UnicodeEncodeError generally occurs while encoding a Unicode string into a certain coding. Only a limited number of Unicode characters are mapped to strings. Thus, any character that is not-represented / mapped will cause the encoding to fail and raise UnicodeEncodeError. To avoid this error use the encode(utf-8) and decode(utf-8) functions accordingly in your …

Python Unicode Encode Error Read More »

How to Solve Python “TypeError: ‘int’ object is not iterable”?

It’s quite common for your code to throw a typeerror, especially if you’re just starting out with Python. The reason for this is that the interpreter expects variables of certain types in certain places in the code. We’ll look at a specific example of such an error: “typeerror: ‘int’ object is not iterable”. Exercise: Run …

How to Solve Python “TypeError: ‘int’ object is not iterable”? Read More »

How To Split A String And Keep The Separators?

Summary: To split a string and keep the delimiters/separators you can use one of the following methods: Use a regex module and the split() method along with \W special character. Use a regex module and the split() method along with a negative character set [^a-zA-Z0-9]. Use a regex module and the split() method along with …

How To Split A String And Keep The Separators? Read More »

Python Comments — 2-Minute Guide with Exercise

Wouldn’t reading code be much easier if the author constantly shared their thoughts with you? Commenting is good practice in Python because it helps others (and your future self) understanding your code much better. Writing commented code makes you more productive in the long term! There are two types of comments: one-line comments and multi-line …

Python Comments — 2-Minute Guide with Exercise Read More »

How To Extract Numbers From A String In Python?

Summary: To extract numbers from a given string in Python you can use one of the following methods: Use the regex module. Use split() and append() functions on a list. Use a List Comprehension with isdigit() and split() functions. Use the num_from_string module. Extracting digits or numbers from a given string might come up in …

How To Extract Numbers From A String In Python? Read More »

Python One Line Docstring

A docstring describes a module, function, class, or method in plain English to help other coders understand the meaning better. You must define the docstring at the beginning of the module, function, class, or method definition. By doing so, the docstring becomes the __doc__ special attribute of that object. You can access the docstring of …

Python One Line Docstring Read More »

Python One Line FizzBuzz

The FizzBuzz problem is a common exercise posed in code interviews to test your proficiency in writing simple Python code. Problem: Print all numbers from 1-100 to the shell with three exceptions: For each number divisible by three you print “Fizz”, For each number divisible by five you print “Buzz”, and For each number divisible …

Python One Line FizzBuzz Read More »

Python One Line Function Definition

A lambda function allows you to define a function in a single line. It starts with the keyword lambda, followed by a comma-separated list of zero or more arguments, followed by the colon and the return expression. For example, lambda x, y: x+y calculates the sum of the two argument values x+y in one line …

Python One Line Function Definition Read More »

How to Access an Object Attribute Given the Attribute Name as a String?

You may know the following problem: You have an object’s attribute name as a string—say, you’ve read it from a file—and you want to access the attribute with the given name. But you cannot use the syntax object.”attribute” because the dot notation only allows for name-access like this: object.attribute. How do you resolve this problem? …

How to Access an Object Attribute Given the Attribute Name as a String? Read More »

How Can I Read Inputs as Numbers in Python?

Are you ready to take your Python knowledge to the next level? Let’s have a little fun with the inputs. Only instead of having you enter your name, we’ll work with numbers. You can play with this program in our interactive shell: Exercise: Run the program—is the result what you expected? It’s a nice, simple …

How Can I Read Inputs as Numbers in Python? Read More »

Python String Formatting: How to Become a String Wizard with the Format Specification Mini-Language

Python provides fantastic string formatting options, but what if you need greater control over how values are presented? That’s where format specifiers come in.  This article starts with a brief overview of the different string formatting approaches. We’ll then dive straight into some examples to whet your appetite for using Python’s Format Specification Mini-Language in …

Python String Formatting: How to Become a String Wizard with the Format Specification Mini-Language Read More »