Want to format a #Python datetime as a string? Use strftime: dt = datetime.datetime(2026, 4, 1, 13, 15, 17) dt.strftime('%Y-%m-%d') # '2026-04-01' dt.strftime('%Y-%m-%d %H:%M:%S') # '2026-04-01 13:15:17' dt.strftime('%y-%B-%e %H:%M:%S') # '26-April- 1 13:15:17'
Format Python datetime as string with strftime
More Relevant Posts
-
Want to parse a #Python string into a datetime? Use strptime, passing (a) the string and (b) the date format spec: datetime.datetime.strptime('2026-04-01', '%Y-%m-%d') datetime.datetime.strptime('26-April-01', '%y-%B-%d') Both return datetime.datetime(2026, 4, 1, 0, 0)
To view or add a comment, sign in
-
-
If a #Python string contains an ISO-formatted date, skip strptime and use datetime.fromisoformat: import datetime as dt dt.datetime.fromisoformat('2026-04-12') # returns datetime at midnight dt.datetime.fromisoformat('2026-04-12 12:34:56') # returns datetime at 12:34:56
To view or add a comment, sign in
-
-
𝗔 𝗣𝘆𝘁𝗵𝗼𝗻 𝗯𝗲𝗵𝗮𝘃𝗶𝗼𝗿 𝘁𝗵𝗮𝘁 𝗰𝗮𝗻 𝗹𝗲𝗮𝗱 𝘁𝗼 𝘂𝗻𝗲𝘅𝗽𝗲𝗰𝘁𝗲𝗱 𝗯𝘂𝗴𝘀. In Python, mutable default arguments can behave unexpectedly. def add_item(item, my_list=[]): my_list.append(item) return my_list print(add_item(1)) print(add_item(2)) Output: [1] [1, 2] Why? Because the default list is created only once and reused. 𝗙𝗶𝘅: def add_item(item, my_list=None): if my_list is None: my_list = [] my_list.append(item) return my_list 𝗞𝗲𝘆 𝗜𝗻𝘀𝗶𝗴𝗵𝘁: Default mutable objects persist across function calls. That’s why unexpected bugs happen. #Python #PythonTips #Programming #Developers #Coding #Tech #LearningPython
To view or add a comment, sign in
-
-
🔄 Input & Output in Python input() → take user input print() → show output Use case: Take server name → display status Simple but powerful. #PythonBasics
To view or add a comment, sign in
-
🚀 Truthiness and Falsiness in Conditional Statements (Python) In Python, values other than True and False can also be evaluated in conditional statements. Values like empty strings, zero, empty lists, and None are considered 'falsy', meaning they evaluate to False in a boolean context. Non-empty strings, non-zero numbers, and non-empty lists are considered 'truthy', evaluating to True. This allows for concise conditional checks based on the presence or absence of data. #Python #PythonDev #DataScience #WebDev #professional #career #development
To view or add a comment, sign in
-
More from this author
Explore content categories
- Career
- Productivity
- Finance
- Soft Skills & Emotional Intelligence
- Project Management
- Education
- Technology
- Leadership
- Ecommerce
- User Experience
- Recruitment & HR
- Customer Experience
- Real Estate
- Marketing
- Sales
- Retail & Merchandising
- Science
- Supply Chain Management
- Future Of Work
- Consulting
- Writing
- Economics
- Artificial Intelligence
- Employee Experience
- Workplace Trends
- Fundraising
- Networking
- Corporate Social Responsibility
- Negotiation
- Communication
- Engineering
- Hospitality & Tourism
- Business Strategy
- Change Management
- Organizational Culture
- Design
- Innovation
- Event Planning
- Training & Development