Data Types: Strings
A string is a sequence of characters. (A string can be a text, a sentence or multiple sentences, It could be a word or a single character)
Let's create a new string and learn something cool.
- Let's create a new variable called myString
We can see the data type of our variable using the type() function.
- As you can see 'str' stands for string which means text!
- How did Python know we passed a string?
- ans: It's inside a quotation marks " "
Note; Strings should always be inside quotation marks! And if you pass a number inside quotation marks it would be stored as a string not an integer!
- lets try 40 in quotation marks and checck the data type.
- Actually this is the correct way of storing numbers that are not used for calculation. eg. kkamenyoh111@gmail.com, an invoice number or a zip code!
- We can also use a single quotation mark ' ' , you just can't mix them. When you start with a single quotation mark, you should end with a single quotation mark and not double as this will result in problems.
- These can be useful when we want to use quotation marks inside our string.
Example:
You can see that " meet me at 5 " turned to color black instead of green.
Python thinks the string ends at " She said ", it does understand what your trying to do afterwards.
So if you want to do something like that you can use single quotation marks outside the text, so you can use double inside.
I'm glag you're following. If you don't understand anything just leave it in the comment section and I will respond.
Okay let's continue.....
In a string every character has it's own index, starting from zero.
We can access an index by using braces [ ].
Steps
1. Use the name of the variable
2. Using braces we can tell the index we want to get
3. Let's get the first, second and last character
- A string also has a length(the number of characters).
- We can get the length of a string by using the len( ) function.
- Meaning we have 25 characters in our string.
More example:
With these 3 characters we know that our first character is index 0, second is 1, and third is 2.
So no matter the length of our string the last character is going to be our [ length - 1 ]
And it is going to work no matter the size of our string.
This formula is going to be very important when we start treating loops in this course!
Slicing
- We can also get part of a string using a technique called slicing.
Example:
Let's say we have a string that represents a bank account number, where the first two characters indicate the country.
As you can see first index [0 : 2] which is G is included but the second index [0 : 2] which is 2, is not not included. So we got from G to 2 but not including 2!
Example 2:
Lets put the country code in the end.
And that's how we do it.
You can try and have some more fun as much as you want.
We already talked about concatenation using the + sign.
ex.
- We concatenated with space as seen above.
- But we have to be careful when we have different data type.
ex. 👉👉👉👉➡
- You can use either the str() function to convert the number to a string before concatenating or simply use the string quotation marks.
Now we know the basics of strings. We will learn more about strings as we advance in the course.
SUMMARY
Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:06:47) [MSC v.1914 32 bit (Intel)] on win32 Type "copyright", "credits" or "license()" for more information. >>> myString = " Some text " >>> #checking data type using the type function >>> type(myString) Output: <class 'str'> >>> myString = "40" >>> type(myString) Output: <class 'str'> >>> myString = ' She said " meet me at 5 " ' >>> myString Output: ' She said " meet me at 5 " ' >>> myString[0] Output: ' ' >>> myString[1] 'S' >>> # let's get the last character, the last character of a string is -1 >>> myString[-1] Output: ' '
See you in our next tutorial. Good luck!
No comments:
Post a Comment