python - cancatenate two strings in a dict -


views.py

 def method(request):          member in list:             personname = {'id':member.id,                          'name':member.firstname  +  member.lastname                        }          return render(request,'some.html',{'name':personname})    

in above views.py,i used concatenation operator concatenate firstname , lastname.concatenation happening,i need make space between firstname , lastname.i used " " giving syntax error.

you can use + concatenating firstname, space , lastname strings:

personname = {'id':member.id,               'name': member.firstname  + " " + member.lastname} 

or use string formatting:

personname = {'id':member.id,               'name': '%s %s' % (member.firstname, member.lastname)} 

or format:

personname = {'id':member.id,               'name': '{0} {1}'.format(member.firstname, member.lastname)} 

Comments

Popular posts from this blog

image - ClassNotFoundException when add a prebuilt apk into system.img in android -

I need to import mysql 5.1 to 5.5? -

Java, Hibernate, MySQL - store UTC date-time -