Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

paste0 like function in python for multiple strings

What I want to achieve is simple, in R I can do things like

paste0("https\\",1:10,"whatever",11:20),

how to do such in Python? I found some things here, but only allow for :

paste0("https\\",1:10).

Anyone know how to figure this out, this must be easy to do but I can not find how.

like image 926
Jason Goal Avatar asked Jun 02 '18 03:06

Jason Goal


People also ask

How do you append two strings in Python?

Two strings can be concatenated in Python by simply using the '+' operator between them. More than two strings can be concatenated using '+' operator.

What is the difference between paste and paste0?

paste () — this function concatenates the input values and returns a single character string. paste0 () — this function concatenates the input values in a single character string. The difference between: paste and paste0 is that paste function provides a separator operator, whereas paste0 does not.

How do you paste a function in Python?

paste() method is used to paste an image on another image. This is where the new() method comes in handy. Parameters: image_1/image_object : It the image on which other image is to be pasted.

What is Paste () in R?

paste() method in R programming is used to concatenate the two string values by separating with delimiters. Syntax: paste(string1, string2, sep=) Return: Returns the concatenate string.


1 Answers

@Jason, I will suggest you to use any of these following 2 ways to do this task.

✓ By creating a list of texts using list comprehension and zip() function.

Note: To print \ on screen, use escape sequence \\. See List of escape sequences and their use.

Please comment if you think this answer doesn't satisfy your problem. I will change the answer based on your inputs and expected outputs.

texts = ["https\\\\" + str(num1) + "whatever" + str(num2) for num1, num2 in zip(range(1,10),range(11, 20))]

for text in texts:
    print(text)

"""
https\\1whatever11
https\\2whatever12
https\\3whatever13
https\\4whatever14
https\\5whatever15
https\\6whatever16
https\\7whatever17
https\\8whatever18
https\\9whatever19
"""

✓ By defining a simple function paste0() that implements the above logic to return a list of texts.

import json

def paste0(string1, range1, strring2, range2):
    texts = [string1 + str(num1) + string2 + str(num2) for num1, num2 in zip(range1, range2)]

    return texts


texts = paste0("https\\\\", range(1, 10), "whatever", range(11, 20))

# Pretty printing the obtained list of texts using Jon module
print(json.dumps(texts, indent=4))

"""
[
    "https\\\\1whatever11",
    "https\\\\2whatever12",
    "https\\\\3whatever13",
    "https\\\\4whatever14",
    "https\\\\5whatever15",
    "https\\\\6whatever16",
    "https\\\\7whatever17",
    "https\\\\8whatever18",
    "https\\\\9whatever19"
]
"""
like image 163
hygull Avatar answered Nov 14 '22 23:11

hygull