R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.

Strings

Strings are a Type and a Class in Python. This means that they have values, methods, operators, and functions that they work with or contain. These are all things we will explore in further detail below.

Values

First letโ€™s take a look at the values that Strings can be. Every String has some number of characters inside of it. Characters are another Type of value, ex: 1, a, B, #, etc.

"Hello World!" - Double Quotes used only

'Hello World!' - Single Quotes used only

"Hel'o World!" - Double Quotes containing one or more Single Quotes

'Hel"o World!' - Single Quotes containing one or more Double Quotes

Some characters have certain abilities.

"Hello World \ !"

"Hello World \n !"

"Hello World \t !"

"Hello World \r !"

\ is an escape character, \n is a new line, \t is a tab and \r is a carriage return. Try them all out.

You probably noticed that in the first example "Hello World \ !" the escape character \ did not show up. If you are trying to create a String that has this character you will need to make it a double backslash.

"Hello World \\ !"

Before we move on to variables I would like to demonstrate exactly what I meant by a String having characters inside of it. Think of each String like an array of boxes filled in by character values. [h][e][l][l][o][!] Each of these boxes has an index, and those indices can be used to look up the values inside of them.

"Hello World!"[1] - Try this out, what value do you get back?

Python and many other programming languages use 0 as their starting point for lists. This will be useful to remember for challenges, and other types in the future! Go ahead and play around with this functionality. All you need to do is put [number] at the end of a String.

Splicing

Splicing is a very important tool Strings and lists have. Try the below examples.

"Hello World!"[0:2] - 0th position up to but not including the 2nd position

"Hello World!"[0::2] - 0th position up to and including the last position using a 2 step

"Hello World!"[11:0] - 11th position up to but not including the 0th position (default step is 1, which means it can never reach 0)

"Hello World!"[::-1] - reverses the string

"Hello World!"[11:0:-1] - 11th position up to but not including the 0th position using a -1 step

"Hello World!"[11:0:-2] - 11th position up to but not including the 0th position using a -2 step

"Hello World!"[11::-1] - 11th posiiton up to and including the 0th position using a -1 step

Simply put, splicing is a way of taking a portion of a String or list based on the what you put inside of the [] at the end of a String or list.

Variables

Strings can be put into variables. Variables are incredibly important for making you code modular, automated, and easily tested.

You can utilize any of the examples in the previous section and assign them to a variable. These varialbe has a type of String and has all the same capabilities that the Strings above did, they just can be used in place of the full String.

text = "Hello World!"

h = "Hello World!"[0]

text

h

Type these all into your Python Interpreter. As you can see this is a powerful way to make your code shorter, easier to follow, and easier to change.

Methods

These are great tools that are built into Strings. I will show you a few, and follow the link if you would like to look at more! https://www.w3schools.com/python/python_ref_string.asp Put all of these examples into your Python interpreter.

"Hello World!".lower()

text.lower()

"hello world!".title()

text.title()

"hello world!".upper()

text.upper()

text

What you should notice is that these methods are not actually changing the String variable text that you made. Thus, if you were wanting to replace text with all of the characters in lower case you would have reassign the variable. Be careful when doing this because writes over whatever value was there before and of whatever type it was before.

text = text.lower()

Go ahead and play around with reassigning variables, and using the different methods in the examples and link above. Just remember to use a method you need to have a . a method name like lower and both parentheses () or the method will give you an error.

Functions

Just like methods, there are built in functions in Python - follow the link to look into them. https://www.w3schools.com/python/python_ref_functions.asp