Let's Learn Python! Volume 2 - Strings, & String Functions
Hello World :]
Alright guys, so were going to kick this volume off with another fairly simple bit of info, but remember, simple is almost always useful! So since we covered integers and math, I’d like to talk with you all now about something called ‘Strings’. A String is a Data Container, designed to contain Sentences, Words, Letters, Characters, Numbers, etc. If this is at all confusing, I hope this next chapter will help you along, So get out your IDLE’s and lets get coding! (Refer to Volume One if you are unsure of what an IDLE is.)
Strings and Simple String functions
So as mentioned in the previous paragraph, Strings will be used to hold sentences and the like. They can also contain symbols and numbers as well; most any keyboard character. Lets start off by just entering text into the IDLE, But remember, you will need to encase your Strings with quotations like “this”, so that the interpeter knows we are entering a string. (Single quotes can work, but you will see why not you should not always use them in the following example.) I will now provide some code from my IDLE, The lines in Bold Italic, will be what is returned to me by the interpreter.
'Hello World'
'Hello World'
'he's got the coffee'
SyntaxError: invalid syntax
"He's got the coffee"
"He's got the coffee”
Alright so now that we’ve covered single quotes vs double quotes, Another important thing to learn about strings is escaping characters. Escaping characters is done by placing a back-slash directly behind them. There are many escapable characters that can be used to direct the interpreter, but for now we will just touch on ‘Newline’. Often times you will want to print out text on several different lines, and the newline command can be used to do this within one line of code. Newline can be initiated by escaping the letter n, as such ‘\n’, as you will see in the following example.
print('1\n2\n3')
1
2
3
As you can see, this will come in handy in many many tools. One of the last things I wanted to show you guys considering strings, is a bit interesting. Some simple string mathematics, something I personally LOVE Python for, You can add strings together, and even multiply them! Code will follow :)
word1 = "Hello"
word2 = "World"
word1 + word2
‘HelloWorld’
word1 + " " + word2
'Hello World'
word1 * 4
'HelloHelloHelloHello'
Okay then, lets go through this together. In the first two lines, we created and assigned two Variables, (word1 and word2) with the respective values of “Hello” and “World”.
In the next two lines, we ask our python interpreter to add these words together, and unfortunately, that’s exactly what it does!
Now in the third set of lines, you can see that we have added an extra blank space character into the equation, and are returned a properly formatted statement. Finally in the last two lines, we see that we are even able to multiply words, which can come in handy when you need a 512 character word while writing a buffer overflow. This is part of the reason I fell in love with Python. Coming up next, we will still be dealing with Strings, only there we will be learning some functions and things we can do with them.
Intermediate String Functions
Allright guys, so we just finished learning what Strings are, and now I’d like to take the chance to show you a few different things we can do with Strings. Okay so the first thing I wanna show you is the ‘len()’ function. The‘len’ function can be used to tell the length of any string, from start to finish. A short example will follow. As usual, the code in Bold Italic, will be what is returned to me through the prompt.
String = “Hello World!”
len(string)
12
As you can see, the Interpreter has returned to us the value 12, representing the number of characters including whitespaces, and symbols. This is a handy feature, when users are entering different information, to be stored in variables and worked with further. Another thing I’d like to go over in this chapter are something called ‘Sub-Strings’. Sub-Strings, as you would probably imagine, are tiny fragments of a String. They can be sized in any way you like, as you will see in the following examples.
string = "Hello World!"
string[0]
‘H’
string[11]
‘!’
Now in the preceding four lines of code, we are creating sub-strings of the string “Hello World!”. The first line entered, requests character 0 of the string “Hello World!”. The system starts counting at 0, rather than 1, thus the letter ‘H’ is returned to us as our value. In the last line, we are asking for the 11’th character, and we are returned the final character in our string, the ‘!’.
string[0:5]
‘Hello’
string[6:12]
‘World!’
In these preceding lines of code above, we are asking our interpreter to return to us a larger selection of characters within our substring. The first line requests the first to fifth characters, and is correctly returned the string ‘Hello’, and the second line askf for characters 6-12, and is returned by the system, ‘world!’.
string[-2]
‘d’
string[-11]
‘e’
The last thing I wanted to show you, was that you can also do these same operations in reverse. If you wanted the second last character, all you would have to do is ask for -2 from the end of the string. In the preceding four lines of code, we ask for the character two places away from the end of the string, and the character 11 places away, and we are returned in respective, the characters ‘d’, and ‘e’.
It’s a little hard to imagine where this can come in useful, but trust me as these lessons go on you will be using everything I have taught you so far over and over. The next volume will be getting quite a bit more interesting and useful in my opinion, so keep an eye out for my next piece, If, Else, and Elif! I will be carrying on working, and releasing the next volume tomorrow at the latest. I hope this has been educational, entertaining, and I hope to see you all there!
Happy Hunting
Cerulean
really useful information regarding strings and functional.
Thanks, will be getting more and more interesting as we go :) Hope your enjoying
UPVOTE! https://steemit.com/news/@bible.com/6h36cq
Congratulations @ceruleanblue! You have completed some achievement on Steemit and have been rewarded with new badge(s) :
Award for the total payout received
Award for the number of comments received
Click on the badge to view your Board of Honor.
If you no longer want to receive notifications, reply to this comment with the word
STOP
To support your work, I also upvoted your post!
Do not miss the last post from @steemitboard!
Participate in the SteemitBoard World Cup Contest!
Collect World Cup badges and win free SBD
Support the Gold Sponsors of the contest: @good-karma and @lukestokes