Hướng dẫn append variable name python

I have gone through almost all answers from stack but not found answer.. Im making program to make sql insertion query.. so i have:

a="Aadi Aarav Aarnav Aarush Aayush Abdul Abeer Abhimanyu Abhiramnew Aditya Advaith Advay Advik Agastya Akshay Amol Anay Anirudhnew Anmol Ansh ArinArjun Arnav Aryan Atharv Avi Ayaan Ayush Ayushman Azaan Azad Daksh Darsh Dev Dev Devansh Dhruv Farhan Gautam Harsh Harshil Hredhaan Isaac IshaanJainew Jason Kabir Kalpit Karan Kiaan Krish Krishna Laksh LakshayManannew Mohammed Nachiket Naksh Nakul Neel Om Parth Pranav PraneelPranit Pratyush Rachit Raghav Ranbir Ranveer Rayaan Rehaannew Reyansh Rishi Rohan Ronith Rudranew Rushil Ryan Sai Saksham Samaksh SamarSamarth Samesh Sarthak Sathviknew Shaurya Shivansh Siddharth Tejas Vedant Veer Viaannew Vihaan Viraj Vivaan Yash Yug Zayan"
name=a.split(' ')
location="Goregaon dadar andheri vileparle santacruz london usa newYork Malad Mulund VAshi Newzealand ".split(' ')
grade="A B C D".split(' ')
query=[]
def generate(id):
    global query
    if id==1:
        query.append('[choice(name)]')
    elif id==2:
        query.append(',randint(35,99)')
    elif id==3:
        query.append(',choice(location)')
    elif id==4:
        query.append(',randint(25000,90000)')
    elif id==5:
        query.append(',choice(grade)')
    elif id==0:
        done()


def done():
    for i in range(5):
         print((query))
        #Result.insert(END,)

The output is::

['[choice(name)]', ',randint(35,99)', ',choice(location)', ',choice(location)', ',randint(25000,90000)']
['[choice(name)]', ',randint(35,99)', ',choice(location)', ',choice(location)', ',randint(25000,90000)']
['[choice(name)]', ',randint(35,99)', ',choice(location)', ',choice(location)', ',randint(25000,90000)']
['[choice(name)]', ',randint(35,99)', ',choice(location)', ',choice(location)', ',randint(25000,90000)']
['[choice(name)]', ',randint(35,99)', ',choice(location)', ',choice(location)', ',randint(25000,90000)']

But I wanted output is value of that variable like random name generated how can I

edit1:

Guys my desired output is like this..

insert into student values (101, 'varun', 62, 'B', 'vashi') ;
insert into student values (102, 'nana', 46, 'C', 'vashi') ;
insert into student values (103, 'varun', 56, 'A', 'vashi') ;
insert into student values (104, 'rajkumar', 81, 'D', 'vashi') ;
insert into student values (105, 'varun', 73, 'A', 'malad') ;
insert into student values (106, 'rajkumar', 49, 'A', 'vashi') ;
insert into student values (107, 'rajkumar', 37, 'D', 'malad') ;
insert into student values (108, 'deepika', 30, 'C', 'vashi') ;
insert into student values (109, 'varun', 37, 'D', 'goregaon') ;

i have a gui program like this..enter image description here

so the number of variables are not fixed/.. when user click on the buttons it make list of variables.. in list then i want to iterate over this.. hope u get my problem,,

❮ Python Glossary

Nội dung chính

  • String Concatenation
  • Related Pages
  • Find Your Bootcamp Match
  • How to Concatenate Strings in Python
  • Python Concatenate Strings Using +
  • Concatenate Strings Python with + Example
  • Concatenate Strings Python Using “%”
  • % Operator Example
  • How do you concatenate numbers in Python?
  • Is there a concatenate function in Python?
  • How do you concatenate a string and a number in Python?
  • What is concatenation in Python example?


String Concatenation

String concatenation means add strings together.

Use the + character to add a variable to another variable:

Example

x = "Python is "
y = "awesome"
z =  x + y
print(z)

Try it Yourself »

Example

Merge variable a with variable b into variable c:

a = "Hello"
b = "World"
c = a + b
print(c)

Try it Yourself »

Example

To add a space between them, add a " ":

a = "Hello"
b = "World"
c = a + " " + b
print(c)

Try it Yourself »

For numbers, the + character works as a mathematical operator:

Example

x = 5
y = 10
print(x + y)

Try it Yourself »

If you try to combine a string and a number, Python will give you an error:

Example

x = 5
y = "John"
print(x + y)

Try it Yourself »



Python Variables Tutorial Creating Variables Variable Names Assign Value to Multiple Variables Output Variables Global Variables


❮ Python Glossary

Python string concatenation is the process of merging two or more strings. The + operator adds a string to another string. % lets you insert a string into another string value. Both operators are used to concatenate strings in Python.


When you’re working with a string in Python, you may want to combine it with another string. For example, you may have a user’s first name and surname and want to combine them together to get the user’s full name.

Find Your Bootcamp Match

  • Career Karma matches you with top tech bootcamps
  • Access exclusive scholarships and prep courses

Select your interest
First name

Last name

Email

Phone number

By continuing you agree to our Terms of Service and Privacy Policy, and you consent to receive offers and opportunities from Career Karma by telephone, text message, and email.

We call merging strings together string concatenation. Python offers a number of string methods that can be used to concatenate separate strings and return a new one.

In this tutorial, we are going to discuss how to use the + and % operators to concatenate strings in Python.

How to Concatenate Strings in Python

You can concatenate strings in Python using the + and % operators. The + operator adds a value to the end of a string whereas the % operator adds a value to any position in a Python string.

Python Concatenate Strings Using +

The + operator lets you combine two or more strings in Python. This operator is referred to as the Python string concatenation operator. The + operator should appear between the two strings you want to merge.

The syntax for the + operator is:

print("Hello " + "World")

This code concatenates, or merges, the Python strings “Hello ” and “World”. The Python print() statement displays the final string to the console.

Notice that we added a space after the word “Hello “. This is because concatenation does not automatically add a space between the strings we merge.

Optionally, we could assign our strings to string variables. This is useful if you are working with multiple strings that you want to merge.

The + operator returns a new string. This is because strings are immutable. which means they cannot be changed. Any string method in Python returns a new string, no matter what format method you use. The string that is created can be assigned to a new variable.

Concatenate Strings Python with + Example

Let’s say that you are working for the payroll department at a company. You want to print out an employee’s contact information to the console. Instead of just printing out the name of the employee and their address, you want to return the information with labels.

You could use string concatenation to merge labels with the employee’s contact information to get your desired output. Here’s an example of a program that merges this information:

print("Name: " + "John")
print("Address: " + "San Francisco")

Our program returns the following output:

Name: John
Address: San Francisco

As you can see, our two strings on each line have been concatenated with the operator +. On our first line, the string Name: John was created, and on the second line, the string Address: San Francisco was created.

In the above example, we added white space to the end of our first strings (“Name: “ and “Address: “). This is not done by default, as we discussed earlier.

Converting a Value to an Integer

The + string operator can only be used to merge two string values. You cannot combine data of two different data types using concatenation.

So, if you have an integer and a string that you want to combine, you’ll need to convert the integer to a string. Here’s what happens if we try to combine a string and an integer:

Our code returns the following error:

TypeError: can only concatenate str (not "int") to str

You can learn more about how this error works and why it is raised in our “Python typeerror: can only concatenate str (not “int”) to str Solution” article.

We can fix this error by using the str() method to convert the integer value to a string. Our code will return the intended output:

Our code returns: John22.

Concatenate Strings Python Using “%”

The % string formatting operator merges a string into another string. This operation is commonly called Python string interpolation. You can add a string to any position in an existing string using interpolation.

Whereas the + operator adds a value to the end of a string, the % operator can add a value to at position that you specify.

Let’s look at the syntax for the % operator:

print("%s %s" % ("Hello", "World"))

This code creates a single string with the contents “Hello World”. The %s values represent a value from the tuple that appears after our string. We specify the values that should appear in the string after the % which follows our string.

We did not use specify a space in any of the values to add to our string. This is because we separated each %s value with a space in our main string.

"Career Karma entered my life when I needed it most and quickly helped me match with a bootcamp. Two months after graduating, I found my dream job that aligned with my values and goals in life!"

Venus, Software Engineer at Rockbot

Let’s use an example to illustrate how the % operator works.

% Operator Example

Say that we have two names that we want to appear in a string. Here’s the code we could use to add those names to our string:

jack_and_jill = "%a and %b went up the hill." % ("Jack", "Jill")
print(jack_and_jill)

Our code returns:

Jack and Jill went up the hill.

We use the % operator to declare where we want a value to appear in our string.

We use %a to say that we want a specific value to appear in that position. %b tells our program that we want another value to appear in that position. Then, at the end of our string, we use the % operator and tell our code what values we want to appear in our list.

We have told our code that we want Jack to be placed in the first open place in our code. Jill should appear in the second open place. That leaves us with the string Jack and Jill went up the hill.

Conclusion

String concatenation is a common operation in many programming languages where you combine two or more strings into one. For example, you may want to combine an employee’s first name and surname into one string, or two different flavors of pizza into one.

In this tutorial, we discussed how you can use the + operator to concatenate strings in Python. We also explored how you can use the % operator to interpolate strings in Python.

To learn more about Python, read our complete How to Learn Python guide.

How do you concatenate numbers in Python?

If you want to concatenate a string and a number, such as an integer int or a floating point float , convert the number to a string with str() and then use the + operator or += operator.

Is there a concatenate function in Python?

Python Concatenate Strings Using + The + operator lets you combine two or more strings in Python. This operator is referred to as the Python string concatenation operator.

How do you concatenate a string and a number in Python?

Python Concatenate String and int.

Using str() function. The easiest way is to convert int to a string using str() function. ... .

Using % Operator. print("%s%s" % (s, y)).

Using format() function. We can use string format() function too for concatenation of string and int. ... .

Using f-strings..

What is concatenation in Python example?

Concatenating means obtaining a new string that contains both of the original strings. In Python, there are a few ways to concatenate or combine strings. The new string that is created is referred to as a string object. In order to merge two strings into a single object, you may use the + operator.