prepend a string to the beginning of another string in python

To prepend a string s1 to the beginning of another string s2 in Python, you can use slicing and concatenation. Here's how to do it:

main.py
s1 = "Hello, "
s2 = "world!"

s3 = s1 + s2[0:len(s2)]
print(s3)
64 chars
6 lines

Output:

main.py
Hello, world!
14 chars
2 lines

In the above code, we first define the string s1 as the prefix that needs to be prepended to s2. We then concatenate s1 and a slice of s2 starting from index 0 to the length of s2. This effectively adds s1 to the beginning of s2. Finally, we print the resulting string s3.

gistlibby LogSnag