Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

combine two strings in python

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..

like image 839
sof Avatar asked Dec 15 '19 18:12

sof


People also ask

How do you combine two strings?

Concatenation is the process of appending one string to the end of another string. You concatenate strings by using the + operator.

What is the best way to concatenate strings in Python?

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.

How do you join text in Python?

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.

How do I concatenate two strings with a space in Python?

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.


1 Answers

try

y = str(i) + str(x)

it should works.

like image 60
Zig Razor Avatar answered Sep 20 '22 00:09

Zig Razor