While it is quite fun to play with kittens and strings, it's even more fun to play with Python and strings! A string in programming speak is an ordered series of characters. That sounds a whole lot like words to me! Let's play with some strings!
To write a string, take any set of characters and put either double or single quotation marks around it:
To Python, there is no difference between using ' and ", as long as you use the same one for that string. Nobody likes mismatched bookends. (Not sure that's a good example, as nobody really likes bookends to begin with.)
We can also assign strings to a variable!
and add them!
Let's add a space between those two:
We can even type whole sentences, punctuation and all:
If we need to use an apostrophe in the middle of our sentence, we can either use the double quotation marks:
or we can escape the apostrophe with a backslash (\) so that Python doesn't get confused.
See? Just as fun as kittens! And to make sure you get that warm and fuzzy feeling: Mister Rogers' Neighborhood Introduction Theme Song
To write a string, take any set of characters and put either double or single quotation marks around it:
>>> 'hi' 'hi' >>> "howdy" 'howdy'
To Python, there is no difference between using ' and ", as long as you use the same one for that string. Nobody likes mismatched bookends. (Not sure that's a good example, as nobody really likes bookends to begin with.)
We can also assign strings to a variable!
>>> hi_string = 'hi' >>> hi_string 'hi'
and add them!
>>> hi_string + 'howdy' 'hihowdy'
Let's add a space between those two:
>>> hi_string + ' ' + 'howdy' 'hi howdy'
We can even type whole sentences, punctuation and all:
>>> taunt = "Your mother was a hamster and your father smelt of elderberries!" >>> taunt 'Your mother was a hamster and your father smelt of elderberries!'
If we need to use an apostrophe in the middle of our sentence, we can either use the double quotation marks:
>>> "It's a wonderful day in the neighborhood." "It's a wonderful day in the neighborhood."
or we can escape the apostrophe with a backslash (\) so that Python doesn't get confused.
>>> 'It\'s a wonderful day in the neighborhood.' "It's a wonderful day in the neighborhood."
See? Just as fun as kittens! And to make sure you get that warm and fuzzy feeling: Mister Rogers' Neighborhood Introduction Theme Song
No comments:
Post a Comment