Tuesday, January 22, 2019

Python Basics 2 - String Operations, Concatenation and More!

In the previous lesson I introduced you to the most basic operation in Python which is the print statement. It is very useful for telling what things are and for what you program is doing. In this lesson we are going to talk about a operations on strings.

Strings are a datatype in Python that hold things like characters, numbers or basically anything that you want to put in them. They are enclosed in quotation marks. We will speak more about different data types in a later lesson.

Have a look at the following code:


print("Strings are wrapped in double quotes")
print('Or they can be wrapped in single quotes')
print('1000')
print("Beware the savage jaw of 1984")
print(',./<>:;[]{}!@#$%^&*()-_=+~`')


When you run the file you should get this output:


Strings are wrapped in double quotes
Or they can be wrapped in single quotes
1000
Beware the savage jaw of 1984
,./<>:;[]{}!@#$%^&*()-_=+~`

Strings can be wrapped in either double or single quotes and everything in the above file is a string. You can even have most special characters in your code except for a few that hold special meaning. More on this later.

We can bring two strings together by using the + operator. We can also bring them together with the , (comma) operator, but this will also add a space between the strings.

Type out the following code exactly as I have written it.


print("Strings are wrapped" + "in double quotes")
print("Strings are wrapped", "in double quotes")
print('Or they can', 'be wrapped in ' + 'single quotes')
print('1000' + '1000')
print("Beware the ", "savage jaw of", "1984")


You should get this output:


Strings are wrappedin double quotes
Strings are wrapped in double quotes
Or they can be wrapped in single quotes
10001000
Beware the  savage jaw of 1984

Notice the lack of space between wrapped and in in the first line of output and the extra space between the and savage in the last line of output. Also note that the numbers did not get added, but simply moved together.

Now add these three lines to the program:


print("-" * 15)
print("Strings are wrapped" + "in double quotes")
print("Strings are wrapped", "in double quotes")
print('Or they can', 'be wrapped in ' + 'single quotes')
print("This is fun " * 3)
print('1000' + '1000')
print("Beware the ", "savage jaw of", "1984")
print("-" * 15)


To get this output:


---------------
Strings are wrappedin double quotes
Strings are wrapped in double quotes
Or they can be wrapped in single quotes
This is fun This is fun This is fun
10001000
Beware the  savage jaw of 1984
---------------

The multiplier is a operator that repeats the string the number of times that you indicate. I would like to show you one more thing.

Have a look at the following program:


print("You cannot add an integer to a string" + 3)


Which produces this output:


 File "tut2.py", line 1, in <module>
   print("You cannot add an integer to a string" + 3)
TypeError: can only concatenate str (not "int") to str

Oh no! We have encountered an error here. Why do you think we got this error? It is because of the fact that we are attempting to concatenate a string to integer. This is not possible. You can only concatenate a string with another string. We will speak more about different data types in a following lesson.

Exercises:

  1. Create several strings, concatenate them together and print them out
  2. We know that “3” + “3” will output 33. What do you think 3 + 3 (without the quotes) will output? Why don’t you try it?

1 comment:

  1. The age of modern day learning has arrived. It is no longer a matter of whether we want to integrate technology and education; it is a necessity. The reality poses an immense problem and threat to the longstanding educational institutions that have for the most part remained unchanged for nearly a century. Who would have imagined that the alphabet's letter "E" would forever transform the face of education to E-Ducation? Programming Help

    ReplyDelete

MIT Intro to CS and Programming PS1B

Here is the solution to problemset1 question B of 6.0001 entitled Introduction to Computer Science and Programming in Python (Fall 2016). ...