Please list 10 significant historical events in chronological order, including WWI, WWII, the 1979 Iranian Revolution, the 1991 dissolution of the Soviet Union, and the 1989 fall of the Berlin Wall. Provide only the year and the event name, without descriptions. Additionally, provide Python code demonstrating how to extract the year from a date string (e.g., '1991').
Assistant
Significant Historical Events in Chronological Order:
1914 - World War I begins
1917 - Russian Revolution
1939 - World War II begins
1945 - World War II ends
1969 - Apollo 11 Moon Landing
1979 - Iranian Revolution
1989 - Fall of the Berlin Wall
1991 - Dissolution of the Soviet Union
2001 - September 11 Attacks
2020 - COVID-19 Pandemic declared
Python Code to Extract the Year from a Date String:
If you have a date string and want to extract the year, you can use Python's datetime module. Here's how to do it:
python
from datetime import datetime
# Example date string
date_string = 'December 25, 1991'
# Define the format of your date string
date_format = '%B %d, %Y' # For example, 'December 25, 1991'
# Parse the date string into a datetime object
date_object = datetime.strptime(date_string, date_format)
# Extract the year from the datetime object
year = date_object.year
print(year) # Output: 1991
Explanation:
from datetime import datetime: Imports the datetime class from the datetime module.
date_string = 'December 25, 1991': The date string you want to extract the year from.
date_format = '%B %d, %Y': The format of the date string.
%B: Full month name (e.g., December)
%d: Day of the month as a zero-padded decimal (e.g., 25)
%Y: Year with century as a decimal number (e.g., 1991)
datetime.strptime(date_string, date_format): Parses the date string into a datetime object according to the specified format.
year = date_object.year: Extracts the year from the datetime object.
print(year): Displays the extracted year.
Note: If your date string is simply a year like '1991', you can directly convert it to an integer:
python
date_string = '1991'
year = int(date_string)
print(year) # Output: 1991
Using Regular Expressions:
If the format of the date string is unknown or varies, you can use the re module to extract the year:
python
import re
# Example date string with unknown format
date_string = 'Event occurred on Dec 25, 1991.'
# Regular expression pattern to find a four-digit year
pattern = r'\b(19|20)\d{2}\b' # Matches years from 1900 to 2099
# Search for the pattern in the date string
match = re.search(pattern, date_string)
if match:
year = int(match.group())
print(year) # Output: 1991
else:
print('Year not found in date string.')
Explanation:
import re: Imports the regular expressions module.
pattern = r'\b(19|20)\d{2}\b': A regex pattern that matches any four-digit number starting with 19 or 20.
re.search(pattern, date_string): Searches for the pattern in the date string.
match.group(): Retrieves the matched year.
int(match.group()): Converts the year string to an integer.
Remember: Modify the date_format or regex pattern according to the format of your date strings.