Mohammad Elsayed
Mohammad Elsayed

Reputation: 2066

I can't use method upper python

I am new to python, I was following a course taking every detail cuz I know that missing any detail in the beginning will lead to big problems but I failed to avoid them anyway, here is the thing I am using python 3.6 and pyCharm as the editor, the instructor uses method method called 'upper("string")' to convert a string letters into capital it caused a problem with him at the beginning but he used the intention action and imported the following library (or what ever it is called in python)

from django.template.defaultfilters import upper

I tried to search for the solution but all solutions didn't work for me, I installed django and virtualenvwrapper using command lines:

1.pip install django
2.pip install virtualenvwrapper-win

,then restarted pyCharm, but the problem still exists.

any solution I will appreciate it.

Upvotes: 0

Views: 1840

Answers (2)

Chris
Chris

Reputation: 22963

As others have said, it's extreme overkill to install a web framework to uppercase a string. Simply use str.upper:

>>> string = 'abc'
>>> string.upper()
'ABC'
>>> 

However, if you don't want to call .upper as a method (for some reason?), you can call the str.upper method directly and pass in a string object:

>>> string = 'abc'
>>> str.upper(string)
'ABC'
>>> 

The upper method defined in django.template.defaultfilters seems to simply be a wrapper around the str.upper method that converts anything passed in to a string object automatically:

@register.filter(is_safe=False)
@stringfilter
def upper(value):
    """Convert a string into all uppercase."""
    return value.upper()

Upvotes: 2

jtagle
jtagle

Reputation: 302

Man, installing django for using upper is like killing a fly with a tank. Django is a framework for web development. I think you are looking for

"Hello World".upper()

That would give you "HELLO WORLD".

That because thats a method of the string class, not a python function. (I'm not quite sure if upper exist as a python built-in function)

Upvotes: 3

Related Questions