![]() This is a common problem that arises when dealing with different bits of data from different places. Strings cannot be concatenated together with integers. In the example above we can repeat the variable combined five times by adding an asterisk followed by 5 which will then have the word elephant printed out five times. We can also repeat a string multiple times without having to type in every time we need the string repeated. Now we have a space between the two strings. If we wanted to have a space in our string, we would simply just add in space and we can concatenate these together because they’re all strings. Now there’s no gap between the two strings that we have just concatenated. So if we add another string called ‘age’ we can combine the two strings using the plus sign. We can simply just need to reassign the variable to this to a new string to get rid of it. That means that once it’s been assigned, we can’t actually change it. ![]() It’s worth noting that a string is immutable. ![]() However, unlike when accessing elements from the start of a string the last element of a string is represented by index -1. We can also access elements at the end of a string using negative integers. So the first ‘e’ in this case will be zero. Normally when indexing we use the square brackets and then we would use the index number to reference each letter. Strings can also be indexed just in the same way we would do with a list or a dictionary in Python. We can simply just type print and then the name of our variable. This is particularly useful for commenting out large blocks of code or maybe writing something at the top of your script that you need to reference to print this out. And the good thing about the triple quote marks is that it allows us to write strings that span through multiple lines. Triple quote marks can also be used to create strings in Python. However, we can also use double quote marks but we can’t mix double quotes with single quotes in a single string. Strings are commonly created using single quotes: For instance, if we create the variable ‘animal’ this is going to be the variable that we’re storing our string in. In Python, a string is created by creating a variable where we’re going to save our string which is basically just some characters put together into words or other things.Ĭreating a variable simply involves initializing a variable that is going to store our string. A string is a sequence of characters that are enclosed by a single or double quote.
0 Comments
Leave a Reply. |