Sequence Data Operations : Concatenation
Syntax : '+' , '+='
Now we will learn some of the most fundamental sequence data operation that is concatenation
and multiplication. So, this will be helpful when you are looping over your looping over
elements in your containers. So, now we will learn some of the most fundamental sequence data
operations that is concatenation and multiplication which can be applied on to any sequential
data.
So, we are going to do concatenation using the plus operator.
And there are a few ways of doing this depending on what you are trying to achieve and the
simplest and the most common method is to use the plus symbol to add multiple strings together
or to add multiple elements together it can be applied onto any sequential data as well. And
simply place a plus between as many as simply place a plus between as many strings as you want
to join together. So, let us see an example to do that. So, for example, so, we know what is there
under strsample it just has a word learning.
So, this is the original list that we have 1 2 a sam 2 and in the next line I am concatenating the
list to the existing list lstsample and the list contains a string that is p y I am giving that inside the
square bracket because you will be able to concatenate to list. So, if you can see here the py list
has been added as has been added as an element to the existing list that is lstsample. So, we do
not have a separate list which contains a py string rather it has been added or gets updated in the
original list itself or gets updated as a last element in the original list itself.
And let us see an example on how we can concatenate the arrays let us see an example whether
we will be able to concatenate two different sequence data for example let us try an example to
concatenate the list and the arrays together. So, first let us see what is there under the array that it
has basically values one two three and four which are of integer data type and by using a plus
operator I am going to concatenate a list together I am going to concatenate a list to the existing
array
So, it basically throws you what traceback errors it basically throws you a type error saying that
it can only append array to list it cannot append array to a. So, the type error is throwing
basically it saying can only append array to array not list to array. So, we because we are trying
to concatenate or we are trying to append the list to an existing array that is not possible. So,
what we have to do here means if you want to concatenate to an array
If you want to concatenate a sequence data to an array then that sequence data should be of the
same kind that is array. So, what we can do here is we can use the same values but just put them
as array basically using the array function and keeping the data type as integer and giving the
values as array and giving the values as list inside the array. Array sample will get concatenated
with the given values that is 50 and 60 since you created it as array and then concatenated.
So, the next one is concatenation on concatenating two tuples together. So, here if you can see
here I have just used plus and equal to then it means that I am going to add a new tuple to the
existing tuple, tupsample and that is automatic glue and that gets updated in the tupsample itself.
So, the tupsample basically had 1, 2, 3, 4 and py and I have added th, on separately as a and I
have added a new tuple having two strings th and on. So, and that get add and that gets added to
the existing tupsample and that gets updated on the existing tuple sample because I have given
plus and equal to.
Join us: https://t.me/jupyter_python
0 Comments:
Post a Comment