Search code examples
pythonsplice

Splicing sections of words to make a new word


I am getting the correct word for this but how do I make it a coherent sentence. Currently it has spaces between each variable.

  • What I get is : NIC E J ob!
  • What I want is: Nice Job!
import string
x = "NICK" # First 3 characters
y = "DE JAVU" # 1 to 4 characters
z = "Bob!" # Last 3 characters

x1 = x[0:3]
y1 = y[1:4]
z1 = z[-3:]


def main():
    print(x1,y1,z1)

main()

Solution

  • x = "NICK" # First 3 characters
    y = "DE JAVU" # 1 to 4 characters
    z = "Bob!" # Last 3 characters
    
    
    def main():
        raw_line = x[0:3] + y[1:4] + z[-3:]
        result = " ".join(map(str.capitalize, raw_line.split()))
        print(result)
    
    main()
    
    >>> 'Nice Job!'