- Python Basics
- Python - Home
- Python - Overview
- Python - History
- Python - Features
- Python vs C++
- Python - Hello World Program
- Python - Application Areas
- Python - Interpreter
- Python - Environment Setup
- Python - Virtual Environment
- Python - Basic Syntax
- Python - Variables
- Python - Data Types
- Python - Type Casting
- Python - Unicode System
- Python - Literals
- Python - Operators
- Python - Arithmetic Operators
- Python - Comparison Operators
- Python - Assignment Operators
- Python - Logical Operators
- Python - Bitwise Operators
- Python - Membership Operators
- Python - Identity Operators
- Python - Operator Precedence
- Python - Comments
- Python - User Input
- Python - Numbers
- Python - Booleans
- Python Control Statements
- Python - Control Flow
- Python - Decision Making
- Python - If Statement
- Python - If else
- Python - Nested If
- Python - Match-Case Statement
- Python - Loops
- Python - for Loops
- Python - for-else Loops
- Python - While Loops
- Python - break Statement
- Python - continue Statement
- Python - pass Statement
- Python - Nested Loops
- Python Functions & Modules
- Python - Functions
- Python - Default Arguments
- Python - Keyword Arguments
- Python - Keyword-Only Arguments
- Python - Positional Arguments
- Python - Positional-Only Arguments
- Python - Arbitrary Arguments
- Python - Variables Scope
- Python - Function Annotations
- Python - Modules
- Python - Built in Functions
- Python Strings
- Python - Strings
- Python - Slicing Strings
- Python - Modify Strings
- Python - String Concatenation
- Python - String Formatting
- Python - Escape Characters
- Python - String Methods
- Python - String Exercises
- Python Lists
- Python - Lists
- Python - Access List Items
- Python - Change List Items
- Python - Add List Items
- Python - Remove List Items
- Python - Loop Lists
- Python - List Comprehension
- Python - Sort Lists
- Python - Copy Lists
- Python - Join Lists
- Python - List Methods
- Python - List Exercises
- Python Tuples
- Python - Tuples
- Python - Access Tuple Items
- Python - Update Tuples
- Python - Unpack Tuples
- Python - Loop Tuples
- Python - Join Tuples
- Python - Tuple Methods
- Python - Tuple Exercises
- Python Sets
- Python - Sets
- Python - Access Set Items
- Python - Add Set Items
- Python - Remove Set Items
- Python - Loop Sets
- Python - Join Sets
- Python - Copy Sets
- Python - Set Operators
- Python - Set Methods
- Python - Set Exercises
- Python Dictionaries
- Python - Dictionaries
- Python - Access Dictionary Items
- Python - Change Dictionary Items
- Python - Add Dictionary Items
- Python - Remove Dictionary Items
- Python - Dictionary View Objects
- Python - Loop Dictionaries
- Python - Copy Dictionaries
- Python - Nested Dictionaries
- Python - Dictionary Methods
- Python - Dictionary Exercises
- Python Arrays
- Python - Arrays
- Python - Access Array Items
- Python - Add Array Items
- Python - Remove Array Items
- Python - Loop Arrays
- Python - Copy Arrays
- Python - Reverse Arrays
- Python - Sort Arrays
- Python - Join Arrays
- Python - Array Methods
- Python - Array Exercises
- Python File Handling
- Python - File Handling
- Python - Write to File
- Python - Read Files
- Python - Renaming and Deleting Files
- Python - Directories
- Python - File Methods
- Python - OS File/Directory Methods
- Object Oriented Programming
- Python - OOPs Concepts
- Python - Object & Classes
- Python - Class Attributes
- Python - Class Methods
- Python - Static Methods
- Python - Constructors
- Python - Access Modifiers
- Python - Inheritance
- Python - Polymorphism
- Python - Method Overriding
- Python - Method Overloading
- Python - Dynamic Binding
- Python - Dynamic Typing
- Python - Abstraction
- Python - Encapsulation
- Python - Interfaces
- Python - Packages
- Python - Inner Classes
- Python - Anonymous Class and Objects
- Python - Singleton Class
- Python - Wrapper Classes
- Python - Enums
- Python - Reflection
- Python Errors & Exceptions
- Python - Syntax Errors
- Python - Exceptions
- Python - try-except Block
- Python - try-finally Block
- Python - Raising Exceptions
- Python - Exception Chaining
- Python - Nested try Block
- Python - User-defined Exception
- Python - Logging
- Python - Assertions
- Python - Built-in Exceptions
- Python Multithreading
- Python - Multithreading
- Python - Thread Life Cycle
- Python - Creating a Thread
- Python - Starting a Thread
- Python - Joining Threads
- Python - Naming Thread
- Python - Thread Scheduling
- Python - Thread Pools
- Python - Main Thread
- Python - Thread Priority
- Python - Daemon Threads
- Python - Synchronizing Threads
- Python Synchronization
- Python - Inter-thread Communication
- Python - Thread Deadlock
- Python - Interrupting a Thread
- Python Networking
- Python - Networking
- Python - Socket Programming
- Python - URL Processing
- Python - Generics
- Python Libraries
- NumPy Tutorial
- Pandas Tutorial
- SciPy Tutorial
- Matplotlib Tutorial
- Django Tutorial
- OpenCV Tutorial
- Python Miscellenous
- Python - Date & Time
- Python - Maths
- Python - Iterators
- Python - Generators
- Python - Closures
- Python - Decorators
- Python - Recursion
- Python - Reg Expressions
- Python - PIP
- Python - Database Access
- Python - Weak References
- Python - Serialization
- Python - Templating
- Python - Output Formatting
- Python - Performance Measurement
- Python - Data Compression
- Python - CGI Programming
- Python - XML Processing
- Python - GUI Programming
- Python - Command-Line Arguments
- Python - Docstrings
- Python - JSON
- Python - Sending Email
- Python - Further Extensions
- Python - Tools/Utilities
- Python - GUIs
- Python Useful Resources
- Python Compiler
- NumPy Compiler
- Matplotlib Compiler
- SciPy Compiler
- Python - Programming Examples
- Python - Quick Guide
- Python - Useful Resources
- Python - Discussion
Python time asctime() Method
Before we discuss the working of the Python time asctime() method, let us understand the different ways time is represented in.
In Python time module, time can be represented in two simple ways: as a tuple or as an object. A tuple is more abstract; it stores the date and time in an order where each element of the tuple represents a specific element of time. For example, (2023, 1, 9, 10, 51, 77, 0, 9, 0) denotes the time "Mon Jan 9 10:51:77 2023". But here, one cannot decipher which element denotes what just by looking at the tuple. This is why an object is introduced to have a clearer understanding of these elements.
An object, usually represented as struct_time, defines various fields to hold various elements of time. Hence, it becomes easier to access them. For example, an object represents the time as (tm_year=2023, tm_mon=1, tm_mday=9, tm_hour=10, tm_min=51, tm_sec=77, tm_wday=0, tm_yday=9, tm_isdst=0).
The asctime() method converts this tuple or struct_time representing a certain time, either in UTC or local time, to a 24-character string of the following form: 'Mon Jan 9 10:51:77 2023'. In this string, the day field is allotted two characters, so if its a single-digit day, space is padded before it.
However, unlike the asctime() C function, this method does not add a trailing newline character to the string.
Syntax
Following is the syntax for the Python time asctime() method −
time.asctime([t]))
Parameters
t − (Optional) This is a tuple of 9 elements or struct_time representing either a UTC time or a local time.
Return Value
This method returns 24-character string of the following form: 'Tue Feb 17 23:21:05 2009'.
Example
The following example shows the usage of the Python time asctime() method. In here, we are trying to represent the current local time of India, returned by the localtime() method, in a 24-character string format using the asctime() method.
import time t = time.localtime() lc = time.asctime(t) print("Current local time represented in string:", lc)
When we run above program, it produces following result −
Current local time represented in string: Mon Jan 9 11:07:30 2023
Example
This method also converts the UTC time into a string format.
In this example, the current UTC time is retrieved using the gmtime() method and this UTC time is passed as an argument to the asctime() method. The current time is converted from the struct_time object to the 24-character string.
import time t = time.gmtime() gm = time.asctime(t) print("Current UTC time represented in string:", gm)
Let us compile and run the program above, to produce the following result −
Current UTC time represented in string: Mon Jan 9 06:05:52 2023
Example
If no argument is passed to this method, the resultant string represents the local time by default.
import time ct = time.asctime() print("Current time represented in string is", ct)
Current time represented in string is Mon Jan 9 11:40:07 2023
Example
The return values obtained from the localtime() and gmtime() methods are in the form of a struct_time object; to pass the argument of this method as a tuple, it has to be created manually as shown in the example below.
import time # Create a tuple representing the current local time t = (2023, 1, 9, 11, 40, 57, 0, 9, 0) # Pass this tuple as an argument to the method ct = time.asctime(t) # Display the string output print("Current local time represented in string", ct)
Current local time represented in string Mon Jan 9 11:40:57 2023