zfill(): add zeros to a string in Python
Use the zfill() to insert zeros 0 at the beginning of a string. The amount of zeros is given by the number passed as argument minus the number of chars in the string. The word ‘beach’ has 5 characters, which…
Use the zfill() to insert zeros 0 at the beginning of a string. The amount of zeros is given by the number passed as argument minus the number of chars in the string. The word ‘beach’ has 5 characters, which…
Use the center() method to center a string. word = 'beach' number_spaces = 32 word_centered = word.center(number_spaces) print(word) #'beach' print(word_centered) ##output: ' beach ' Notice the spaces in the second string. The word ‘beach’ has 5 characters, which gives us…
Bitwise Operators allow you to perform operations on binary numbers. The values are automatically converted to binary and then the logic is applied to them. The output is also converted back from binary implicitly. They are: &: AND Only the…
Let’s see a program to check if a number is either even or odd. The logic is simple: take a number and divide it by 2, if the remainder is 0, then the number is even, otherwise the number is…
In this post, we will learn how to find the largest number. The first program uses the if statement to achieve that. The second program, the recommended way, uses the built-in max function and a list. Code using the if…
In this post, we will learn how to find the smallest number. The first program uses the if statement to achieve that. The second program, the recommended way, uses the built-in min function and a list. Code using the if…
Use the expandtabs() method to set the number of spaces for a tab. You can set any number of spaces, but when no argument is given, the default is 8. Basic Usage my_string = 'B\tR' print(my_string.expandtabs()) #output: B R Notice…
Let’s see how two perform a very common and simple task when learning a programming language: adding two numbers. In python the code for adding two numbers is very straightfoward. Adding numbers directly first_number = 32 second_number = 8 total…
Use the istitle() method to check if the first character in every word in a string is upper case and the other characters are lower case. text = 'This is a regular text' print(text.istitle()) #output: False text = 'This Is…
There are two scopes: local and global. Global Scope A global scope allows you to use the variable anywhere in your program. If your variable is outside a function, it has a global scope by default. name = "Bob" def…