Python Built-in Functions

How to Print a String Without ‘\n’ in Python

Strings, Printing and ‘\n’ The printing of a basic string is probably the first program the majority of people, myself included, write in Python –  does print(‘hello world’) sound familiar? From the outset we know the principle is simple, we can pass our string through the print() function and the output will be displayed in …

How to Print a String Without ‘\n’ in Python Read More »

How to Check if Items in a Python List Are Also in Another

There comes a time in all our coding lives when we need to compare lists to understand whether items in one list appear in a second list. In this article we’ll start where we all started, using for-loops, before moving to a more classic Python list comprehension. We’ll then move beyond that to use Python …

How to Check if Items in a Python List Are Also in Another Read More »

How to Determine the Type of an Object in Python?

Problem Formulation Every Python object is of a certain type, also called “class”. The class is a blueprint that shows the data and capabilities of each object/instance that is created after this blueprint. Given a Python object (=instance). How to determine/check/get its type (=class)? There are many variants of this question: How to determine the …

How to Determine the Type of an Object in Python? Read More »

How to Find the Longest String in a Python List?

Use Python’s built-in max() function with a key argument to find the longest string in a list. Call max(lst, key=len) to return the longest string in lst using the built-in len() function to associate the weight of each string—the longest string will be the maximum. Problem Formulation Given a Python list of strings. Find the …

How to Find the Longest String in a Python List? Read More »

How to Count the Number of Function Arguments in Python?

💡Introduction You might have a code in Python such that certain functions have no arguments, whereas the others have numerous arguments. While there are times, we have functions with arguments unknown. Thus, we may have n number of arguments in a function, and sometimes we don’t even have a clue about the input size of …

How to Count the Number of Function Arguments in Python? Read More »

Python – How to Check if a Dict Key Exists?

Summary: To check whether a key exists in a dictionary, you can use: The in keyword The keys() method The get() method The has_key() method Overview Mastering dictionaries is one of the things that differentiates the expert coders from the intermediate coders. Why? Because dictionaries in Python have many excellent properties in terms of runtime—and …

Python – How to Check if a Dict Key Exists? Read More »