Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Use range to add char. to empty string
#1
This seems simple enough, but I can't seem to get my head around it.

The problem:
Create an empty string and assign it to the variable lett. Then using range, write code such that when your code is run, lett has 7 b’s ("bbbbbbb").

My attempt:
lett = ' '
for _ in range (7):
     append.lett(b)
print(lett)
I'm pretty sure you can't append to a string, but I'm at a loss for other options. It doesn't seem like we can split lett then rejoin.
Quote
#2
lett = ' ' # This is not an empty string! There is space between quotes.
for _ in range(7):
     append.lett(b) #  You can use `+=`, e.g. lett += 'b'
print(lett)
johneven likes this post
Quote
#3
You are right, strings in Python are immutable, so you can't append to it.

Two options come in mind - create new string with every iteration of loop or append to data structure which is mutable and at the end convert to string.

I just pinpoint that your lett is not empty string:

>>> lett = ' '
>>> len(lett)
1
>>> ' ' in lett
True
>>> lett = ''
>>> len(lett)
0
johneven likes this post
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Life of Brian: Conjugate the verb, "to go" !
Quote
#4
I would also note that b is not the string 'b', unless somewhere you have done b = 'b'.
perfringo and johneven like this post
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures

Quote
#5
In general, the problem statement

Quote:Then using range, write code such that when your code is run, lett has 7 b’s ("bbbbbbb").

slightly confuses me. A minimal solution would be
lett = ''
range
lett = 'b' * 7
We've used range function and assigned desired result to the lett variable.
Formally, everything is done. No one requires us to use for-loop.
johneven likes this post
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  list index out of range mcgrim 2 248 May-25-2019, 07:44 PM
Last Post: mcgrim
  switch case with range jun 3 373 Feb-27-2019, 05:35 AM
Last Post: scidam
  Need to use range with decimals KameronG 7 398 Feb-08-2019, 07:57 PM
Last Post: ichabod801
  why is this not filling my empty list? Siylo 4 372 Jan-21-2019, 05:27 PM
Last Post: Siylo
  IndexError: list index out of range abdullahali 4 457 Jan-17-2019, 07:54 AM
Last Post: buran
  python homework print the sum of a range of numbers from x to y kirito85 3 532 Oct-28-2018, 08:56 AM
Last Post: kirito85
  Python Tutorial range function question HawkeyeKnight 2 487 Sep-27-2018, 09:45 AM
Last Post: ThiefOfTime
  Issues with Inserting Values into an Empty List with a While Loop TommyMer 2 604 Sep-12-2018, 12:43 AM
Last Post: TommyMer
  String index out of range felie04 2 1,238 Aug-17-2018, 11:18 PM
Last Post: felie04
  why is the append function gets empty on the loop?/python rhai 3 714 Jul-07-2018, 12:19 PM
Last Post: rhai

Forum Jump:


Users browsing this thread: 1 Guest(s)