0

I have question about coding in Python, IDE I use is Pycharm Community Edition.

I have code like this

i = 0
str_1 = """public class Schedule_Boolean_Monday_""" + str(i) + """ {

public Schedule_Boolean_Monday_""" + str(i) + """_3(Context context){
    this.mContext = context;
    }
}"""

for i in range(3):
     print(str_1)

And Current Output is like this

public class Schedule_Boolean_Monday_0 {

public Schedule_Boolean_Monday_0_3(Context context){
    this.mContext = context;
    }
}
public class Schedule_Boolean_Monday_0 {

public Schedule_Boolean_Monday_0_3(Context context){
    this.mContext = context;
    }
}
public class Schedule_Boolean_Monday_0 {

public Schedule_Boolean_Monday_0_3(Context context){
    this.mContext = context;
    }
}

public class Schedule_Boolean_Monday_0 and public Schedule_Boolean_Monday_0_3(Context context) do not change. The str(i) in the String does not increment.

I would like to get output like this

public class Schedule_Boolean_Monday_0 {

public Schedule_Boolean_Monday_0_3(Context context){
    this.mContext = context;
    }
}
public class Schedule_Boolean_Monday_1 {

public Schedule_Boolean_Monday_1_3(Context context){
    this.mContext = context;
    }
}
public class Schedule_Boolean_Monday_2 {

public Schedule_Boolean_Monday_2_3(Context context){
    this.mContext = context;
    }
}

Is this possible to increment Integer value inside of the String?? I would love to hear your advice.

Thank you very much

1 Answer 1

2

That's because you assign it while i is 0. It won't update inside the loop. Try putting the string assignment inside the loop instead

for i in range(3):
    str_1 = f"""public class Schedule_Boolean_Monday_""" + str(i) + """ {

    public Schedule_Boolean_Monday_""" + str(i) + """_3(Context context){
        this.mContext = context;
        }
    }"""
    print(str_1)
Sign up to request clarification or add additional context in comments.

1 Comment

I appreciate your answer. I have tried your code and it works perfectly what I'd desire for. Thank you very much

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.