fbpx

Activity 2

Activity: To Concatenate variables in Python

 

1. Open the PyCharm Editor installed on your system. An introductory screen will show up on your system.

2. Click on “Create New Project” for creating your first project and starting to code in Python.

3. Select the location where you wish the project is to be created. You can change the name to something meaningful such as  “NewProject”. Click on “Create”.

4. Next, go to the “File” menu and click on “New”. Click on the option “Python File”.

5. A pop-up window will appear where you can enter the filename.

6. Next, type the program. We have taken “Python” as the string and “100” as the number. If we type the following command, print(“Python” +100) there will be an error (type error) since “100” has not been declared as a string.

7. The code would be as follows:

The following program would return an undefined output:
a= ‘Python’
b=100
print(a + b)

For making this program return a defined output, it should be written as:
a= ‘Python’
b=100
print(a+str(b))
Here, the “str” function takes a variable to be a string.

8. Next, go to the “Run” menu and select “Run” for running the program. The shortcut for this is Alt+Shift+F10.

9. The output of the program will be displayed at the bottom of the screen. Observe it.

Python string concatenation is the process of merging two or more strings. The + operator adds a string to another string. % lets you insert a string into another string value. Both operators are used to concatenate strings in Python.

Try to explore them and other operations of strings and variables!!