Strings and variables


#1



I've tried a number of way and each time I receive a message, "have a created a variable yet?

Write your code below, starting on line 3!

len ("my_string " )
print "the length of my_string "
print "my_string".upper ()

I need to know how to answer the question 3.


Replace this line with your code.


#2

Variable names, while they are technically strings in the raw, are never quoted. That is how Python knows they are identifiers that reference an object or value in memory.

"my_var"  is a string literal

my_var   is a variable, an identifier

To set a variable to a string, we assign a string expression using =

my_var = "this is a string literal"

Note that we can use either double quotation marks, " ", or single, ' '.

To obtain the length of a string, we use the len() function and pass in its variable referrer.

print len(my_var)

#4

This is what I've typed out:

Write your code below, starting on line 3!

line 3 len("my_string")
line 4 print "the length of my_string "
line 5 print "my_string".upper ()

Error I'm receiving:

Oops, try again. Did you create a variable called my_string?

Line 3 is what needs to be fixed, what do I need to write? I've tried multiple methods and still I receive the error.


#5

I've tried that and I still receive an error message of:

Oops, try again. Did you create a variable called my_string?

I'm scratching my head over this one, I will continue even start over and see what I can do. Your help is much appreciated.


#8

How is it we create a variable, again? Just write it out...

my_string

and now assign a string value to it,

my_string = "this is a string expression"

Now the value can be referenced by the variable...

print my_string    # this is a string expression

We can perform operations on the string by applying them to the variable, as well.

print len(my_string)    # 27

print my_string.upper()    # THIS IS A STRING EXPRESSION

Starting to make sense?


#9

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.