Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling class method inside string format

Consider this:

from datetime import datetime

now = datetime.now() 
now.strftime("%p") # returns 'PM'
'{0.day}'.format(now) # returns 22

'{0.strftime("%p")}'.format(now)
# gives
# AttributeError: 'datetime.datetime' object has no attribute 'strftime("%p")'

This seems to imply that I can't call a class method inside the format (I guess that's what strftime is).

What's a workaround for this (assuming I need to call the method inside the string, and keep using a format) ?

like image 965
duff18 Avatar asked Jan 22 '21 14:01

duff18


People also ask

How do you call a function in string?

There are two methods to call a function from string stored in a variable. The first one is by using the window object method and the second one is by using eval() method. The eval() method is older and it is deprecated.

Can you call a function in an F string?

Python f-strings enables us to call functions within it.

What is written by format () method?

In java, String format() method returns a formatted string using the given locale, specified format string, and arguments. We can concatenate the strings using this method and at the same time, we can format the output concatenated string. Parameter: The locale value to be applied on the format() method.

How do you create a function from a string in Python?

This is the way to go. Use an existing parser to parse your string and then have it fill in the variables within a function object. If it's a complete function, then you'll have to get a more powerful parser. Bingo, like PIL's 'tostring' and 'fromstring' methods, liking yamls object creation as well it seems fun.

How do I call the string format method?

However, when calling the String.Format method, it is not necessary to focus on the particular overload that you want to call. Instead, you can call the method with an object that provides culture-sensitive or custom formatting and a composite format string that includes one or more format items.

How do you call a method from the main class?

public class Main { static void myMethod() { System.out.println("Hello World!"); } } myMethod () prints a text (the action), when it is called. To call a method, write the method's name followed by two parentheses () and a semicolon; Inside main, call myMethod ():

How do you format a string in Java?

Java String format () The java string format () method returns the formatted string by given locale, format and arguments. If you don't specify the locale in String.format () method, it uses default locale by calling Locale.getDefault () method. The format () method of java language is like sprintf () function in c language and printf () ...

How do you call a method in Java?

To call a method in Java, write the method name followed by a set of parentheses (), followed by a semicolon (;). A class must have a matching filename (Car and Car.java).


3 Answers

You could do this.

>>> from datetime import datetime
>>> now = datetime.now()
>>> '{0:%p}'.format(now)
'PM'

This will also work with f-strings.

>>> f"{now:%p}"
'PM'
like image 184
Abdul Niyas P M Avatar answered Oct 07 '22 02:10

Abdul Niyas P M


You can use the f-string:

from datetime import datetime

now = datetime.now() 
now.strftime("%p") # returns 'PM'
'{0.day}'.format(now) # returns 22

print(f'{now.strftime("%p")}')

Else:

from datetime import datetime

now = datetime.now() 
now.strftime("%p") # returns 'PM'
'{0.day}'.format(now) # returns 22
print('{0:%p}'.format(now)) 

Documentation

  • strftime() and strptime() Behavior
like image 2
Federico Baù Avatar answered Oct 07 '22 01:10

Federico Baù


You could use f-strings like this:

from datetime import datetime

now = datetime.now() 
now.strftime("%p")
print(f'{now.day}')

print(f'{now.strftime("%p")}')
like image 1
The Pilot Dude Avatar answered Oct 07 '22 01:10

The Pilot Dude