Python Forum
inserting value into strings - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: inserting value into strings (/thread-4608.html)



inserting value into strings - Saxycool12345 - Aug-29-2017

so hi I am a complete noob at python and only started learning the basics I tried to create many random things which seem to work but I have no idea how to make this one work, as I said before I am a total noob so don't scold me
>>> def Love():
B=int(len(input("please put your name:")))
G=int(len(input("please put the name og your love interest:")))
print("your compatibillity is" + B-G + "%...")
p.s i am using python v.3.6.2


RE: help me - wavic - Aug-29-2017

Inside the print function, you are using string concatenation. Since it's a string concatenation B - G produce integer which is not a string.


RE: help me - Saxycool12345 - Aug-29-2017

(Aug-29-2017, 08:43 PM)wavic Wrote: Inside the print function, you are using string concatenation. Since it's a string concatenation B - G produce integer which is not a string.
im sorry but as i said before i am a noob so can you please make it easier to understand for me, i am sooo sorry for being a pain


RE: help me - sparkz_alot - Aug-29-2017

In the print() function you are telling Python to concatenate (add) two strings, 'B' and 'G', yet you have defined them as integers. 

>>> x = 'hello'
>>> y = 'world'
>>> x + y
'helloworld'
>>>
Since you are using Python 3.6.2, you should look into formatting. So your print() function would look like this:

print("your compatibillity is {} %".format(B + G))
Also, you have defined a function "Love", which you do not use. If B, G and "print" are meant to be part of the function, they need to be properly indented and the function called:

def love():
    B = int(len(input("please put your name: ")))
    G = int(len(input("please put the name of your love interest: ")))
    print("your compatibility is {} %".format(B + G))

love()
Output:
C:\Python36\python.exe C:/Python/Games/scratch.py please put your name:joe please put the name og your love interest:sally your compatibility is 8 %



RE: help me - deaspo - Aug-29-2017

Alternatively you can use String formatters, %s
def love():
    B = int(len(input("please put your name: ")))
    G = int(len(input("please put the name of your love interest: ")))
    print("your compatibility is %s" % str(B-G))
 
love()



RE: help me - Saxycool12345 - Aug-29-2017

(Aug-29-2017, 09:11 PM)sparkz_alot Wrote: In the print() function you are telling Python to concatenate (add) two strings, 'B' and 'G', yet you have defined them as integers. 

>>> x = 'hello'
>>> y = 'world'
>>> x + y
'helloworld'
>>>
Since you are using Python 3.6.2, you should look into formatting. So your print() function would look like this:

print("your compatibillity is {} %".format(B + G))
Also, you have defined a function "Love", which you do not use. If B, G and "print" are meant to be part of the function, they need to be properly indented and the function called:

def love():
    B = int(len(input("please put your name: ")))
    G = int(len(input("please put the name of your love interest: ")))
    print("your compatibility is {} %".format(B + G))

love()
Output:
C:\Python36\python.exe C:/Python/Games/scratch.py please put your name:joe please put the name og your love interest:sally your compatibility is 8 %

thx dude ur awesome!! i understand now and i think i will mess around with this... Thx again! Dance Dance


RE: help me - wavic - Aug-30-2017

What I have said is that you adding the result of B - G which is integer to the end of the string using string concatenation - the '+' sign. But in order to do that you have to turn the result to an string first: print("your compatibillity is" + str(B-G) + "%..."). Or just to use string formatting that was proposed already.


RE: help me - Saxycool12345 - Sep-13-2017

i think i finally found out how to do it XP
def Love():
def Love():
	b=int(len(input("put in your name please: ")))
	g=int(len(input("please put in the name of your crush: ")))
	l=int(((((b*4)**4)-b**2)-g**5)+((((g*3)**2)-g**2)-b**4))
	b=int(l/9000)
	print("--------------------------------------------------------------------------")
	print("your love compatibillity is " + str(b) + "%; Disclaimer: this is not accurate at all")
yay


RE: help me - deaspo - Sep-13-2017

Too many
int
, unnecessary.