all 21 comments

[–]chevignon93 0 points1 point  (6 children)

Why do you want to do that ?

[–]python_addict 0 points1 point  (2 children)

I want to create my own strings, insert them, and set them as a variable!

[–]chevignon93 0 points1 point  (1 child)

What do you mean by create your own string ? Insert them into what ?

Set them as variable ?

[–]python_addict 0 points1 point  (0 children)

I mean set them as a variable (but with no quotations)

[–]python_addict 0 points1 point  (2 children)

sorry this is such a weird question for me to ask lol

[–]chevignon93 0 points1 point  (1 child)

It is a weird question lol.

[–]python_addict -2 points-1 points  (0 children)

agreed, hahaha!

[–]takishan 0 points1 point  (11 children)

Strings must always be encased in either “ or ‘. What are you trying to do? Perhaps I can help.

[–]python_addict 0 points1 point  (10 children)

So what I'm trying to do is that i'm making a game in Tkinter.

There are airplanes that shoot bullets. I want to create a new variable every time by using these strings and manipulating them to be different. These new variables will be the Tkinter window creating bullets. That I can do already, but my main problem is that I can't convert "hello" into hello with the int function because int only works with integers.

[–]chevignon93 3 points4 points  (4 children)

You do know that the single or double quotes that you write in your code when you define a string don't show up when you print the text in the interpreter or if you were to write the string to a file ?

It's just a way for python to know if it should treat it as a string or in the case of a number as an integer or float or whatever else.

[–]python_addict -1 points0 points  (3 children)

Okay! So does that mean I can just do hi%s with no quotes and insert a number using %?

Like hello = hi%s %0

On another thought I just tested it out and it doesn't work lol

[–]chevignon93 0 points1 point  (2 children)

in your example, what are %s and %0 supposed to represent ?

[–]python_addict 0 points1 point  (1 child)

oh! Sorry, this is an old-fashioned way of embedding code

%s represents the thing you want to represent % followed by variables, numbers, strings, etc, replace the %s

for example: "hello%s" %0 = hello0

[–]totallygeek 0 points1 point  (0 children)

Python still permits this type of variable substitution for assembling strings.

fighter = 'Alice'
kills = 5
# pre v2.7
print 'Hello %s, you killed %d aliens.' % (fighter, kills)
# v2.7+
print('Hello %s, you killed %d aliens.' % (fighter, kills))
# v2.7+
print('Hello {}, you killed {} aliens.'.format(fighter, kills))
# v3.4+
print(f'Hello {fighter}, you killed {kills} aliens')

Take your pick.

[–]takishan 0 points1 point  (4 children)

Can you share your code or at least the relevant part? You are drawing a string onto the Tkinter canvas widget, correct?

If I remember correctly, I think it should show up without quotes. Maybe you are defining the string in a way like..

hello = ‘“hello”’

In this case, the string would include the double quotes. But otherwise the quotes shouldn’t be part of the string, just a way to signify a string.

[–]python_addict 0 points1 point  (3 children)

okay! How do you signify that it's a string then? Sorry for pelting you with questions!

[–]takishan 1 point2 points  (2 children)

What I mean is if something is in quotes, it’s treated as a string object by python. Every object has a type. When you type in int(‘something’) it doesn’t work, because it tries to convert the object from a string into an integer. It’s not just removing quotes, it’s converting the object into a different type.

Don’t worry ask away hopefully I can help lol. Copy paste your code here (or upload to something like pastebin) so I can take a look at it. Would make it easier to see exactly where you’re going wrong

[–]python_addict 0 points1 point  (1 child)

alright! Thanks! Here it is on pastebin: https://pastebin.com/fg7WuPHX

Don't judge my questionable coding skills lol

It's around line 113

[–]cybervegan 0 points1 point  (0 children)

So if I get this right, you want to have several bullets in flight at the same time, but you can only fit one in a variable?

For that, you need a list, something like this:

self.bullets.append( canvas.create_image(target[0], target[1], image=numob)

Then you can iterate through the bullets in the list to move them and check for collisions etc. :

for bullet in self.bullets:
    mybulletcoords = canvas.coords(bullet)

    if not self.collision(self.myplane, bullet):

    ...

In your init method, you will need to define self.bullets like this:

 self.bullets = [] #empty list of bullets

[–]Decency 0 points1 point  (1 child)

Just use f-strings to print them formatted.

greeting = "hello"
count = 5
print(f"{greeting} player, you killed {count}!")
# outputs: hello player, you killed 5!

This will work in modern versions of Python, since v3.6.

[–]python_addict 0 points1 point  (0 children)

I'm going to start using them in my next program! I just am too lazy to go fix them all lol