Coding3: Strings: Length, Concatenation, Indexing, and Slicing Part 1
What are strings, and some of the extremely basic things we can do them. If you are interested in working with some sort of a Natural Language Processing (NLP) data, read this.
Quick Recap
As of right now we know what variables are, and how we can use them to store some information on our RAM. We also know how to leave little helpful comments so that when we come back to our code several months later, and want to find out what’s going on. We can just read the comments of our code, instead of reading our code’s syntax to find out what’s going on. In this article, we'll talk about a new type of data you can work with called strings.
Chad Tip: When you get a DS/ML/Quant job at a company, make sure you speak with your boss so you know what the proper procedure to document your code is. If you don’t do this, you will end up spending a lot of time later on going back to some of your older code and revamping it to the company’s format. This’ll cause you to lose precious time that you could be using to help make the company more revenue => Bigger Bonus.
Table of Contents:
An Introduction to Strings
Creating Strings
Python
R
String Length
Python
R
Empty String
String Concatenation
Python
R
1 - An Introduction to Strings
As of now, we've worked with actual number data types. However, there's another type of data we can work with. Whenever we load up text to our program, the text is stored as what’s known as a string type. You can think of string as like text from Microsoft word.
All strings have 3 following properties:
Strings have individual letters, numbers, and symbols. These individual entities are called characters.
Strings have a specific length attached to them, the length of a string is the number of characters a string contains.
Characters in a string show up in a specific sequence. The sequence tells us which position each character is located at, and that the order of the characters matter.
If you like, you can see the Wikipedia definition of Strings.
2 - Creating Strings
Python
In order to create a string, you need some text, and you need to surround it in quotes. In Python, you have the option of choosing 3 different type of quotes:
Single quotes: ‘
Double quotes: “
3 Single quotes:’’’
The following code shows you an example of a string that says BANKS ARE ZEROS. Don’t forget, if we don’t use the print() function, there won’t be any output.
Notice the green color change? In your python code, if you spot a bit of green text, that usually indicates it’s a string.
R
R is slightly different, you still need to use quotes in order to create a string, however R does not accept the usage of the 3 single quotes. In other words, the following 2 methods work to create a string:
Single quotes: ‘
Double quotes:”
Once again, below is a screenshot of a string that says BANKS ARE ZEROS.
3 - String Length
From above, the number of characters in a string is called the length of a string. For example, the string ‘69,420’ has a length of 6. The string ‘abc’ has a length of 3, basically you can just count the amount of letters, and symbols between the quotes and that will be the length of a string.
Python
Python has a function called len() that we can use to get the length of a string. Here’s an example below:
R
R has a function called nchar(), it stands for number of characters. We can use this to determine the length of a string. Here’s an example below:
4 - Empty String
Sometimes, when we create string objects, we want them to initially be empty. In other words, when we have a string (str) that is empty, we call that an empty string. To make an this, just use 2 quotes, but put nothing inside them:
abc=''
5 - Concatenate Multiple Strings
The basic concept of string concatenation is that you can create 2 different strings, and then combine (concatenate) them together. For example, we can create string1=’6’, and create string2=’9’, and do some string concatenation to create ‘69’ and combine them. The string concatenation effectively is appending the other string onto the first one.
Python
In Python, we use the + sign to concatenate strings together. Here’s a simple way this works: string1 + string2. Below is a screenshot you can look at.
R
In R, we use the paste function in order to concatenate two strings together. You can use this link to see how the paste() function works. Below is a screenshot you can take a look at that uses the paste() function in order to concatenate the same thing as the Python thing. In the sep parameter, we will leave this as ‘‘ for now.
6 - Exercises
Here's a few exercises you can try out:
Create a string, and an empty string. Then print the length of both of the strings
Create two string, concatenate them, and then print the newly created string.