2

I have been learning Python for two weeks now I created a function which displays your biographical data. I want to know how to put objects of different data types into one string

As you can see, the data includes strings, an int and a boolean. I want to be able to display them as a single string. So far my function only displays them individually like this: ('1000000', 'Bob', 17, False) I want to it display like this: '<1000000,Bob,17,False>'

def student_data(name, age, student_number, enrolled):
    age = int(age)
    true = "true"
    false = "false"
    # check to see if user is enrolled or not.
    if enrolled == true:        
        enrolled = True
    else:        
        enrolled = False
    data = (student_number, name, age, enrolled,) 
    return (data)

data = student_data("Jana", 17, "1001291657", "false")
print (data)
1
  • 2
    Please do not define 'true' and 'false' the way you have. VERY unpythonic. Just use the builtins True and False. Otherwise people will look at you funny... Commented Sep 14, 2014 at 22:18

3 Answers 3

4

The easy answer to the question 'Concatenating objects of different types into a string' is to use map:

>>> list_of_obj=['string', 1234, 123.4, {1:'one'}]
>>> map(str, list_of_obj)
['string', '1234', '123.4', "{1: 'one'}"]

Or a list comprehension:

>>> [str(e) for e in list_of_obj]
['string', '1234', '123.4', "{1: 'one'}"]

In each case, you are applying the built-in function str to each object in a list.

However, may I suggest writing a class to keep track of students:

class Student(object):
    def __init__(self, name, age, number, enrolled=True):
        self.name=name
        self.age=age
        self.number=number
        self.enrolled=enrolled

Then add a method to the class to format it the way you want:

    # Note indentation to be part of the class definition of Student...
    def __str__(self):
        seq=(self.name, self.age, self.number, self.enrolled)
        return '<{}>'.format(', '.join([str(e) for e in seq]))

Then create a student:

>>> student1=Student('Bob Jones', 23, 12345, True)

Then the format is as you define it to be:

>>> print student1   
<Bob Jone, 23, 12345, True>

As stated in the comments, I find it not so Pythonic to redefine true = "true" and false = "false" Just use True and False directly. The main reason, besides not confusing others (or yourself) is that you loose Python's sense of 'truthy' vs 'falsey':

>>> false = "false"
>>> bool(false)
True
Sign up to request clarification or add additional context in comments.

Comments

3

if you just want to show them ,simply use a function like this:

def student_data(name, age, student_number, enrolled):
 print "name:{0}, age:{1}, student_number:{2}, enrolled :{3}".fromat(name, age, student_number, enrolled)

and if you want to convert all of them to one string use this :

def student_data(name, age, student_number, enrolled):
   my_str=str(name)+','+str(age)+','+str(student_number)+','+str(enrolled)
   return my_str

as an alternative answer its better to pass your arguments in a list to function and use map and join built in functions for create a pythonic code for this aim :

my_list = [name, age, student_number, enrolled]
    def student_data(my_list):
     return ','.join(map(str,mylist))

2 Comments

Thanks for the feedback! The second method you showed was very easy to understand (Y)
thanks ... there are many suggestion for a question like this as you see in answers but i answer base on your request ! so i will add another ways in edit to answer !
3

You can create a list of strings by converting your objects like this

[str(i) for i in data]

Then it's just a matter of joining the list together with join() method. Last lines of your code would look like this:

data = student_data("Jana", 17, "1001291657", "false")
string_data = ",".join([str(i) for i in data])
print("<{}>".format(string_data)) # adding brackets with format()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.