• 26 November

    Remove Empty Lines from Text File in Python

    Use isspace() Method To remove empty lines from a Text File in Python: Open a file in read and write mode(r+). Use for loop for iterate over each line in the file. Use isspace() method to check if line is empty. if it is not empty, add it to result. Use seek(0) to move cursor […]

  • 25 November

    Check if Variable Is None in Python

    In Python, Check if Variable Is None and Check if Variable Is null have same solutions as meaning of both queries is same. 1. Introduction In Python Programming, checking when a variable is None(Python equivalent of null or nil in other languages) is a common task, particularly in functions returning no value, handling optional parameters […]

  • 25 November

    Print Elements of List on Separate Lines in Python

    Lists are one of the fundamental data types in Python that is capable of storing several items in one single variable. Elements of the list are all generally printed in a single line. However, there is often a need to print all the elements on separate lines, which is what this article will be focused […]

  • 25 November

    Print String Till Character in Python

    A string can be termed as a collection of characters with each character occupying a particular position. Strings cannot be altered and are considered immutable in Python. Print String Till Character in Python This tutorial will demonstrate how to print string till character in Python. Using the for loop and break statement A string can […]

  • 25 November

    Remove NewLine from String in Python

    1. Introduction Removing newline characters from strings is a common task in Python, especially in text processing and data cleaning. However, newline characters can vary across different operating systems: Unix-like systems (including Linux and macOS) use \n (Line Feed), while Windows uses \r\n (Carriage Return and Line Feed). Therefore, a solution that works uniformly across […]

  • 24 November

    Print Methods of Object in Python

    💡 TL;DR To get all methods of object, use inbuilt dir() method [crayon-662e704a16a7e762575238/] Working with Python methods and attributes is very easy but seems challenging when we want to know if a specific object has a particular method/attribute or not. This tutorial teaches the use of dir(), and inspect.getmembers() to print all methods of a […]

  • 24 November

    Print Variable and String in Same Line in Python

    💡 TL;DR To Print Variable and String in Same Line in Python, use print() method and separate String and variable with comma. [crayon-662e704a172cc827053172/] Print Variable and String in Same Line in Python We use the print() function in Python to display output. It was changed to a function in Python and existed as a statement […]

  • 20 November

    Exit if Statement in Python

    Using break Statement Use the break statement to exit if statement in Python. [crayon-662e704a1744b082024990/] [crayon-662e704a17450527191285/] The break statement is used to terminate the loop prematurely. This statement is commonly utilized in loops to exit from them if a particular condition was met. In the above example, we used a for loop to iterate over a […]

  • 18 November

    How to Remove Decimals in Python

    Quick Solution To Remove decimals in Python, use int() function. [crayon-662e704a1761a935346837/] Or use math.trunc() [crayon-662e704a1761d695140231/] Output 3 Please note that when we remove decimal places, number before decimal point(.) will remain the same. If you want to round it up or down, then you should use round() instead. Two of the most common types to […]