-2

I work on python 2, I tried the code available on this site but nothing seemed to work. this is an example of the code I used, however it didnt work:

def repeat_to_length(string_to_expand, length):
   return (string_to_expand * ((length/len(string_to_expand))+1))[:length]

I am extremely new to coding and using python so i will appreciate anyone can help me point put my mistake

DavidG
  • 24,279
  • 14
  • 89
  • 82
Heba Masarwa
  • 57
  • 1
  • 7

2 Answers2

0

When dividing len() by length you need to ensure (to cast) the result to an integer: Edit: the cast is necessary in Python 3, not Python 2

def repeat_to_length(string_to_expand, length):
   return (string_to_expand * (int(length/len(string_to_expand))+1))[:length]

result = repeat_to_length("hello", 22)
print(result)

The multiplier for a string must be an integer value, not a float.

hellohellohellohellohe

[:length] is sequence slice notation, it takes characters from the beginning of the string up-to length characters.

Andy G
  • 19,232
  • 5
  • 47
  • 69
  • i tried that, however i still got an error message stating that "repeat to length" is not defined – Heba Masarwa Oct 30 '18 at 15:51
  • @HebaMasarwa You must have defined your function after making the function call and that could be a possible reason for `repeat to length" is not defined` – mad_ Oct 30 '18 at 15:52
  • 1
    You can copy and paste my code and run it. Are you calling your function from the same file, and with the correct indentation? – Andy G Oct 30 '18 at 15:52
  • can you please explain the use of `[:length]` in return? It somehow ensures that the output doesn't exceed the given length. – Sandesh34 Oct 30 '18 at 15:55
  • @Sandesh.Patil It will slice the repeated string generated to give first 22 characters in this example – mad_ Oct 30 '18 at 15:57
  • @Sandesh.Patil I added an additional note to the answer. – Andy G Oct 30 '18 at 15:58
  • I did copy and past, and i restarted the shell, but i still get the same error message. I apologize for causing you this much trouble. – Heba Masarwa Oct 30 '18 at 15:58
  • ".. you need to ensure (to cast) the result to an integer .." no OP does not. For in Python 2.7, the code works as provided -- you are (not entirely unexcusable) commenting for a Python 3+ question. – Jongware Oct 30 '18 at 16:00
  • @HebaMasarwa Make sure you are not copy pasting on a shell the entire thing. as it will copy and paste in the shell but not execute it. So in order to execute it on the shell first copy and paste the function part and then function calling part. – mad_ Oct 30 '18 at 16:00
  • Provide the exact code and error details. It is either a misspelling or indenting or... – Andy G Oct 30 '18 at 16:00
  • 1
    @usr2564301 My bad. In which case, we still don't know what the error is, in Python 2. – Andy G Oct 30 '18 at 16:01
-1
string1 = 'abcd'
length = 10
string2 = string1 * length
print(string2)

truncateLength = 15
print(string2[0 : truncateLength])

Original String:

abcdabcdabcdabcdabcdabcdabcdabcdabcdabcd

Truncated String:

abcdabcdabcdabc
Adrish
  • 52
  • 1
  • 7
  • 1
    this isn't what the OP is asking for – wpercy Oct 30 '18 at 15:44
  • The question was "How to repeat a string in python to a certain length". This example shows how a string can be repeated using multiplication. – Adrish Oct 30 '18 at 15:45
  • 2
    @Adrish That was my initial interpretation. The OP wants to return the string repeated, but truncated at a certain length of characters. – Andy G Oct 30 '18 at 15:47