Now the python is running in your systems; let's dive into python without wasting any more time.
Now let's Write Your First program of Printing 'Hello World':
Open you're idle and go to the ‘Files’ tab and open a new file.
Save this file as ‘hello.py.’
and write the code as follows:
And go to the ‘Run’ option on the taskbar and click on run module or you can directly run by pressing F5 key.
The second option is running from the terminal. The following example is given in Linux terminal, but it can be executed from all other operating systems.
Python3 following by the path of the program file. The same command will be run on Mac, but windows use “python” instead of “python3”.
What are Variables in Python:
In python, there is a strict pattern to name the variables. User cannot name a variable in any fashion. We can only use characters, numbers, and underscore.
The number should not be used at the starting of the variable. Following are some example to name the variables:
This is the right way to name a variable.
A syntax error will be generated if we named a variable in the wrong way as following:
What are Strings in Python:
Unlike C, C++, and Java, there is no specific data type before initializing the variable. Python will decide on run time weather the variable is String, Integer or Float.
So, at first, we will have a look at how we can initialize strings.
How to do String Initialization:
In python string can be initialized by the following methods:
How to do String Slicing:
We can slice the strings in python; this procedure is known as string slicing in programming. String slicing accepts the starting index and ending index within square brackets.
Note: The index starts from 0
Following are the examples of slicing the strings:
We can also get the index of a specific word in a string by running the following program:
Following are the indexes at which these words are starting from. We can also get the length of the string by python built-in function len(). It is to be noted that length function starts from 1 whereas the index starts from 0.
What is String Manipulation?:
String manipulation is a technique to change the string according to the need. We can remove the characters from the string, split the string into half, and search for the characters in a string.
How to String.split():
Split functions take one argument and split the string on that argument. If that argument is present in the string, then that string will be splinted; otherwise, nothing will happen. The split function returns the value as a list. We will see in the later part that what is list and how to handle the list.
Strip function takes the argument and removes that value from that string. By default, if no parameter is passed in strip function than it removes the starting and ending spaces of string, otherwise it removes that particular character from that string.
Following is the example of stripping function:
Replace function replace the value from a particular string. It takes two arguments. First one is the character that is to be replaced and the other character that will be the value of that replacement.
Upper and Lower:
Upper and lower function convert the string from lower case to upper case and from upper case to lower case in the following scenario. These are used for string manipulation.
Why are these string manipulations used?
This string manipulation is used to clean the data. In advance topics, when we will be studying data science, this string manipulation will be extensively used to clean the data and organize the data in a format in which we want to see it. Moreover, these are used to see special characters whether they are present in a string or not or to make a long sentence into words as we see in the example of a splitting function.
Concatenation is a process to add two strings to form as one string. In python, there are two different ways to concatenate the strings. One way of concatenating the string is to use plus (+) operator between two strings, and the other way of doing so is to use comma(,) symbol between two strings. The following method is only applicable when printing the two variables.
String formatting is a technique to create a new string by adding the variable without concatenating. There are several ways of formatting the string in python. Following is the way to implement the formatting of the string.
- The first example is called c-style formatting. In this formatting, we define the string value by “%s” and for integer “%d” is used. The way to implement the method is as follows:
- The second example of doing so is by using. Format function. In this method, curly braces are made within a string which is replaced by the variable when it is printed.
- The third technique is more accessible than the examples mentioned above, and we use “f” at the start of the string two specify the formatting, which is as follows:
Please check out Next articles to learn python: