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.
pooky2483
 
Posts: 5
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)
we will be moving to python-forum.io on October 1 2016
more details here
User avatar
metulburr
 
Posts: 2228
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.
pooky2483
 
Posts: 5
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
we will be moving to python-forum.io on October 1 2016
more details here
User avatar
metulburr
 
Posts: 2228
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?
pooky2483
 
Posts: 5
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.
Due to the reasons discussed here we will be moving to python-forum.io on October 1, 2016.

This forum will be locked down and no one will be able to post/edit/create threads, etc. here from thereafter. Please create an account at the new site to continue discussion.
User avatar
micseydel
 
Posts: 2996
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.
pooky2483
 
Posts: 5
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).
Due to the reasons discussed here we will be moving to python-forum.io on October 1, 2016.

This forum will be locked down and no one will be able to post/edit/create threads, etc. here from thereafter. Please create an account at the new site to continue discussion.
User avatar
micseydel
 
Posts: 2996
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 4 guests