Wednesday, 25 May 2016

Learn Python the easy way - 4. String Operators

Input function

  The input function is used to gather input from user. It's mainly useful in actual programming world. This input function prompts the user for input and returns what they entered.

>>>input("What is your favorite programming language:")
What is your favorite programming language: Python



Concatenation is nothing but an addition of two or more strings. This can be done by adding mathematical operators in between the strings.


As you can see, it adds two strings. You can use single quotes or double quotes, both will works. 
In this concatenation, If your string contains numbers it takes as string and not as an integer.

For example,

>>>"43" + "4" + "1"

You can also multiply strings by using integers.

>>>print("Hello" * 4)

Type Conversion

The problem is getting input from user is probably as in the form of strings. In order to make it as an integer, we use the concept of type conversion.
For an example, take the following code;

>>>int(input("Enter a number:")) * int(input("Enter another number:")) 
Enter a number: 3
Enter another number: 2

No comments:

Post a Comment