How do I use strings?

A forum for general discussion of the Python programming language.

How do I use strings?

Postby pooky2483 » Wed Nov 06, 2013 8:04 pm

I am attempting to get into programming in Python. I previously did a course on C and COBOL back in 1992 but nothing came of it. I have since found Pyton and want to get into learning it.

I have set myself a project of trying to create an address database but don't know where to start with creating string variables.

I know a basic way of entering them, such as

Print "Enter First name: ";first$
Print "Enter Second Name: ";second$
full_name$=first$+" "+second$

But don't have a clue with Python.
Image
Stickers are FREE - the ONLY cost is posting a SAE to me
Python 3.2.3 | Kubuntu 12.04
pooky2483
 
Posts: 4
Joined: Wed Nov 06, 2013 7:42 pm
Location: Derby, UK

Re: How do I use strings?

Postby metulburr » Wed Nov 06, 2013 8:09 pm

Code: Select all
full_name$=first$+" "+second$

im not sure what you are doing with the '$' signs, but it looks very C like

In python, you can use the format method to insert variables into the string

Code: Select all
first = 'metul'
second = 'burr'
fullname = '{} {}'.format(first, second)
print(fullname)



you can also do it the old school way, but in python it is not really needed with format method
Code: Select all
first = 'metul'
second = 'burr'
fullname = '%s %s' %(first, second)
print(fullname)
New Users, Read This
version Python 3.3.2 and 2.7.5, tkinter 8.5, pyqt 4.8.4, pygame 1.9.2 pre
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
User avatar
metulburr
 
Posts: 1125
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: How do I use strings?

Postby pooky2483 » Wed Nov 06, 2013 8:14 pm

I want the strings to be entered by the user but don't know how to do it, I know it is a string variable but I am at a loss in Python.
Image
Stickers are FREE - the ONLY cost is posting a SAE to me
Python 3.2.3 | Kubuntu 12.04
pooky2483
 
Posts: 4
Joined: Wed Nov 06, 2013 7:42 pm
Location: Derby, UK

Re: How do I use strings?

Postby metulburr » Wed Nov 06, 2013 8:29 pm

python3.x
Code: Select all
first = input('Enter First name: ')
second = input('Enter First name: ')
fullname = '{} {}'.format(first, second)
print(fullname)


python2.x
Code: Select all
first = raw_input('Enter First name: ')
second = raw_input('Enter First name: ')
fullname = '{} {}'.format(first, second)
print(fullname)


Use whichever version of python you are using
New Users, Read This
version Python 3.3.2 and 2.7.5, tkinter 8.5, pyqt 4.8.4, pygame 1.9.2 pre
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
User avatar
metulburr
 
Posts: 1125
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: How do I use strings?

Postby pooky2483 » Wed Nov 06, 2013 9:19 pm

I'm amazed at how simple it is, how does it know strings from numbers or is that in the
Code: Select all
fullname = '{} {}'.format(first, second)


The {} bits?
Image
Stickers are FREE - the ONLY cost is posting a SAE to me
Python 3.2.3 | Kubuntu 12.04
pooky2483
 
Posts: 4
Joined: Wed Nov 06, 2013 7:42 pm
Location: Derby, UK

Re: How do I use strings?

Postby micseydel » Wed Nov 06, 2013 9:25 pm

The docs on this topic are very good.

You need to go through a basic Python tutorial. Variable assignment and string are surely covered in any one you can find.
Join the #python-forum IRC channel on irc.freenode.net!
User avatar
micseydel
 
Posts: 941
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: How do I use strings?

Postby pooky2483 » Wed Nov 06, 2013 9:29 pm

I've got a few in .PDF format but I just couldn't get my head round the lack of '$' to denote a string variable and numbers.
Image
Stickers are FREE - the ONLY cost is posting a SAE to me
Python 3.2.3 | Kubuntu 12.04
pooky2483
 
Posts: 4
Joined: Wed Nov 06, 2013 7:42 pm
Location: Derby, UK

Re: How do I use strings?

Postby micseydel » Wed Nov 06, 2013 9:32 pm

I can appreciate trying to apply what you already know toward what you're learning, but from an experienced programmer, I'd like to tell you to be very careful about that. After you've mastered Python if you really want your mind blown try Lisp and then Prolog (where you'd learn that you don't necessarily need to be able to modify a variable after it's set to have full programs).
Join the #python-forum IRC channel on irc.freenode.net!
User avatar
micseydel
 
Posts: 941
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA


Return to General Discussions

Who is online

Users browsing this forum: No registered users and 1 guest