I have a list1
like this,
list1 = [('my', '1.2.3', 2),('name', '9.8.7', 3)]
I want to get a new list2
like this (joining first element with second element's second entry);
list2 = [('my2', 2),('name8', 3)]
As a first step, I am checking to join the first two elements in the tuple as follow,
for i,j,k in list1:
#print(i,j,k)
x = j.split('.')[1]
y = str(i).join(x)
print(y)
but I get this
2
8
I was expecting this;
my2
name8
what I am doing wrong? Is there any good way to do this? a simple way..
Concatenation is the process of appending one string to the end of another string. You concatenate strings by using the + operator.
One of the most popular methods to concatenate two strings in Python (or more) is using the + operator. The + operator, when used with two strings, concatenates the strings together to form one.
Python String join() The string join() method returns a string by joining all the elements of an iterable (list, string, tuple), separated by the given separator.
Python concatenate strings with space We can also concatenate the strings by using the space ' ' in between the two strings, and the “+” operator is used to concatenate the string with space.
try
y = str(i) + str(x)
it should works.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With