1

consider this code:

str = "test"
def v():
    for i in str:
        k = k + i
        print(k)

I don't want to assign k anything at all. I used k = None to define empty variable but that doesn't work.

9
  • why doesn't it work? what error do you get? Commented Jul 17, 2014 at 8:25
  • 1
    @sharth Well None+1 is going to cause an error. Commented Jul 17, 2014 at 8:26
  • 1
    If it's a string, empty is the empty string k="" Commented Jul 17, 2014 at 8:26
  • 4
    A side-mistake: do not use str because it is a built-in function in Python (docs.python.org/2/library/functions.html) Commented Jul 17, 2014 at 8:30
  • 1
    @jean-loup empty string worked fine thank you Commented Jul 17, 2014 at 8:32

1 Answer 1

2

Simple as this:

str="test"
def v():
    k = ""
    for i in str:
        k=k+i
        print(k)

Another advice, str is a built-in type in python you maybe would like to change it for:

string="test"
def v():
    k = ""
    for i in string:
        k=k+i
        print(k)
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.