Jamshaid Ali189 Points
Can someone explain this. I didnt understand a single thing in this video can you i dont know explain it so
Can someone explain this. I didnt understand a single thing in this video can you i dont know explain it so i can understand it better please
Craig Davis Python
Mel RumseyTreehouse Staff
Heya Jamshaid Ali! There was definitely a lot of information covered in this short video! 1.) *Quotes " and ' * -- We can use whichever quotes we want to when we create strings. If we want to have a string with single quotes that looks like this:
'Hello, my name is Mel!'
This would be completely fine, and so would double quotes:
"Hello, my name is Mel!"
Either way is completely valid. We would start running into issues, however, if I added a contraction.
'Hey, I'm Mel!'
We would run into an error because it is now thinking the string ends earlier than it does. If we wanted this same string, we could write it with double quotes around the string, and leave the single quote for the contraction.
"Hey, I'm Mel!"
There might come a time where you want to use only single quotes which leads me into the next segment that Craig covered.
2.) escape sequences\' and \n -- Using the same examples above, let's say we want to keep only single quotes.
'Hey, I'm Mel!'
we can use escape sequences or a backslash before the single quote ( \' ) to make this string work.
'Hey, I\'m Mel!'
There are quite a few escape sequences, but we are just covering the \' and the \n. \n is used to add a new line.
newline_string = 'Hey, I\'m Mel!\nAnd this will be on a new line!' print(newline_string)
This will output:
Hey, I'm Mel! And this will be on a new line!
You can check out other escape sequences here!
3.) String combining with + (Concatenation) -- You can also add multiple strings together with concatenation.
'Hey, I\'m Mel' + ' and I am learning' + ' Python'
This will turn become a new string that looks like this:
'Hey I\'m Mel and I am learning Python'
Note: Spaces must be added in the strings
4.) *Reassigning adding a string to a variable or += * -- We can also add strings to a variable containing a string:
who = ''Hey I'm Mel" # If I want to add what I am learning to this string we can do this two different ways who = who + " and I am learning Python" # OR who += " and I am learning Python" # Either way the who variable now contains one string that reads # "Hey I'm Mel and I am learning Python"
You can read more about concatenation here! 5.) Multiplying strings -- If I wanted to add a nice design to the that looks like:
There is a nice shortcut so that we don't have to type a bunch of repeating characters:
menu_design = '+~' * 20 print(menu_design)
This would have the same output as above. Without having to type all of the repeating characters.
Okay, It looks like that is it for this video. I hope these few examples helped give you a different perspective on what Craig was talking about.