wondering how to split a string in python? Python has in-built function split that splits a string. Split Function By default python split function splits string by space and returns list of splitted string. Example str=”Once upon a time” print(str.split(” “)) Output: [“Once”,”upon”,”a”,”time”] Similarly we can pass any separator we want. Example str=”once-upon-a-time” print(str.split(“-“)) Output: […]
Category: Python Problem Solver
How to reverse a string in python?
In certain situation, you may want to reverse the string using Python programming language. Following is the right way to do it. Reverse a string in python In python, there is no in built function available which can reverse a string. However we can use slice in backwards to print the string in the reverse. […]
How to write a comment in python ?
Often we are required to write comments inside a python script. Unlike C,C++,Java,.Net in python, writing a comment is bit different. How to write a comment in Python? In python, we can write single line comment and multiline comment. Single line comment Begins with # Example #this is an example of single line comment Multiline […]
Python Count the number of occurrence of a sub string in a string?
Lets see how to find out the number of occurrence of a sub string in a string. Example: sentence=”This is a lovely day and this day could be your best day, if you smile” Count how many times the word ‘day’ (substring) is appearing in the above sentence (string) Solution 1 sentence=”This is a lovely […]
Python how to check if list contains element?
In python, we can check if a list contains certain element. For this “in” keyword is helpful. Example selected_candidates=[“rahul”,”ajay”,”vijay”] #check if raju is present in the list if “raju” in selected_candidates: print(“Raju has been selected for the job”) else: print(“Raju has been disqualified for the job”) Output: Raju has been disqualified for the job.
Read html file in python beautifulsoup
Beautifulsoup is a powerful library that helps scrape complex to complex html website easily. Python+Requests+Beautifulsoup=Successful Web Scraping Read html file in python beautifulsoup To use beautifulsoup library, this library must be present in the system. If you are using pycharm, then go to file then project settings and install bs4. bs4 contains beautifulsoup library. Following […]
Python How to make a dictionary from two lists?
We have two lists. And we want to create a dictionary combining values of these two lists. In such case, we can create a dictionary from two lists as following way. Example name_list=[“rahul”,”vijay”,”ajay”] phone_list=[“1111″,”2222″,”3333”] #declare an empty dictionary phone_book={} for idnum in range(0,len(name_list)): phone_book.add(name_list[idnum],phone_list[idnum]) #lets print the dictionary print(phone_book) Output: { “rahul”:”1111″, “vijay”:”2222″ “ajay”:”3333″ } […]
Python how to check if string contains substring?
In certain situation, you may want to know whether a sting contains another sub-string. Then the helpful tool that eases the process of knowing this is, “in” keyword. It checks whether the supplied element is present in the target element or not. If element is present then it would return true otherwise it would return […]
How to make python print something multiple times?
Python has print function which prints the passed argument. And in some cases we may want to print a message multiple times. If we want to print something multiple times then for loop is an easy option. for _ in range(0,10): print(“hello friend”) Output: hello friend hello friend hello friend hello friend hello friend hello […]
Check if a python dictionary contains a key
Dictionary are the one of the very powerful data listing option available in python. Unlike list and tuple, dictionary hold data in key and value pair. So in some cases the developer may want to find out if a certain key is present. Lets see how to check if a python dictionary contains a key? […]
Check if a python string contains a character
In some cases we may want to check if string contains a certain character. To do check presence of a character in a string in python, we can use the in keyword functionality. If element is present then it would return true other wise it would return false. Example sentence=”Orange is not apple” #lets check […]
How to go through every character in a string python?
Python treats a string as sequence of characters. Which means in the eyes of python, a string list of characters. Therefore to access a character in a string we must have to call it by its index number. Every Character in a String With the help of for loop we can print every character of […]
Connect MS Access database in python (pyodbc)
MS access is also used as database to store and update data. In python projects we can use ms access as database and for that pyodbc library is required. Install pyodbc library Assuming you have prior known of python and library installation, run the following command in your terminal or command prompt. pip install pyodbc […]
How to select li element in selenium python?
In certain cases we may want to fetch data from list by using Selenium in Python. The simplest solution to target any element by its tag name is to use find_element_by_tag_name. Note: Selenium Python has similar function find_elements_by_tag_name. Do not use this one. It returns a list of targeted element. Select li element in selenium […]
How to launch Internet Explorer IE browser in selenium python?
Assuming you have prior knowledge of Python and Selenium, following are the step by step guideline to launch Internet Explorer browser in Selenium Python. To run internet explorer browser from selenium python, you must have to have internet explorer driver available in your system. You can download it from selenium’s official website. Selenium Official Website: […]
How to add new python interpreter in pycharm?
In mac OS and Linux OS, python older version is pre-installed. So if we run pycharm, then by default pycharm picks older version of python which is 2.7. But we want the latest version 3.0 as interpreter in pycharm. So following is the solution to add new python interpreter in pycharm. Open pycharm and launch […]
How to see python version in Jupyter notebook?
There are plenty chances you may have landed in a situation where the code is not behaving as you have expected and you may want to check the python version that is running behind the Jupyter notebook. Then following is the code you must have to use to find out the python version. # see […]
How to check if python is already installed?
Mac Operating System and Linux operating system has already python installed. Where as certain application needs python, so such application might have installed python in windows as well. So irrespective of the platform it is best to first check if python is already installed. How to check if Python is installed in windows 10? Open […]