-4

I'm wanting to create a string that will, If Word 1 was CHEESE and Word 2 = HAM, create a string looking something like this...

CHEESEHAMCHEESEHAMCHEESEHAMCHEESEHAM etc...

I then want the ASCII values of each character to be taken and be used in a Caesar cipher program.

Thanks in advance, I'm not too experienced with Python.

Byte Commander
  • 6,506
  • 6
  • 44
  • 71
Liam
  • 1
  • 1
  • 3

2 Answers2

1

To concatenate two strings s1 and s2, you use the + operator:

s = s1 + s2

To repeat a string s n (integer number) times, you use the * operator:

ss = s * n

To get a list of integers representing each character of a string ss, you can use the built-in ord() method in a list comprehension:

l = [ord(c) for c in ss]

So a full program using two strings and the number of repetitions (here hard-coded as constants), and with the snippets above compressed into one line, would look like this:

s1 = "CHEESE"
s2 = "HAM"
n = 5

l = [ord(c) for c in (s1+s2)*n]
print (l)
Byte Commander
  • 6,506
  • 6
  • 44
  • 71
  • You're the first one on [SO] who calls me "master"... :D – Byte Commander Dec 07 '15 at 16:14
  • As this answer seems to have helped you and solved your problem, please accept it (click the grey tick on the left of it). Thanks! Also make sure to have read the [tour] page, where you learn the most important things about how this site works in less than two minutes. – Byte Commander Dec 07 '15 at 16:27
0

you can also try this.

wordOne="cheese"
wordTwo="ham"
i=0
n=5
while i<n:
    print("wordOne", end="")
    print("wordTwo", end="")
    i+=1
umuur
  • 91
  • 1
  • 11