start_year = int(input("Enter start year:"))
end_year = int(input("Enter end year:"))
mylist = []
for year in range(start_year,end_year):
if ((year % 4 == 0) and (year % 100 != 0)) or (year % 400 == 0):
mylist.append(int(year))
for i in range(0, len(mylist), 10):
print(*mylist[i:i+10], sep=', ')
eyllanesc 225k18 gold badges130 silver badges198 bronze badges asked Nov 13, 2019 at 1:06
1 1 Answer answered Nov 13, 2019 at 1:12 Peter LeimbiglerPeter Leimbigler 10.3k1 gold badge21 silver badges34 bronze badges
View Discussion Improve Article Save Article ReadDiscussView Discussion Improve Article Save Article By default Python‘s print() function ends with a newline. A
programmer with C/C++ background may wonder how to print without a newline. Python’s print() function comes with a parameter called ‘end‘. By default, the value of this parameter is ‘\n’, i.e. the new line character. Example 1:Here, we can end a print statement with any
character/string using this parameter. Python3print ( "Welcome to" , end = ' ' )
print ( "GeeksforGeeks" , end = ' ' )
Output: Welcome to GeeksforGeeks Example 2: One more program to demonstrate the working of the end parameter. Python3
print ( "Python" , end = '@' )
print ( "GeeksforGeeks" )
Output: Python@GeeksforGeeks Example 3:The print() function uses the sep parameter to separate the arguments and ends after the last argument. Python3print ( 'G' , 'F' , sep = ' ', end=' ')
print ( 'G' )
print ( '09' , '12' , '2016' , sep = '-' , end = '\n' )
print ( 'Red' , 'Green' , 'Blue' , sep = ',' , end = '@' )
print ( 'geeksforgeeks' )
Output GFG
09-12-2016
Red,Green,Blue@geeksforgeeks - Dashboard
- Classroom
-
Interviews
- Solo Courses
- $0 Coins
Methods in Python - Course
- Overview
-
Introduction
- Python For Loops
-
Introduction to For Loops ●
What is a For Loop
●Connection to Iterables ●Syntax and Examples
- Python Dictionaries
-
Introduction to Python Dictionaries
●
What is a Python Dictionary ●Python Dictionary Update
●Common uses for Dictionaries -
Applications of Python Dictionaries
●
Python Dictionary and For Loop
●Python Dictionary Check Membership
-
File Input Output Python
-
Reading and Writing Files in Python ●
Introduction
●Reading a File ●Writing
a File ●Line Counter
-
Strings in Python
- Certificate
Configuration
Replace and Strip Strings
Replace and StripA very useful method is called replace('char_to_replace','replacement') . We use this to get rid of punctuation or any characters that we do not want in our string. For examples, consider the sentence with bad punctuation: 'Hello. World! Lets, get rid of some bad punctuation!'
Here we want to get rid of the period after hello and the comma after lets. To do this, we will use the replace method as is shown below.
string = 'Hello. World! Lets, get rid of some bad punctuation!'
# Replace the comma
string_1 = string.replace(',','')
# Replace the period
string_2 = string_1.replace('.','')
print(string_2)
This gives back Hello World! Lets get rid of some bad punctuation!
Another useful method that we can
use with strings is called .strip() . This will remove specific characters from the end of a string string that we do not want. For example, if you are trying to count the occurrences of words in a string, you want to ignore all the periods and exclamation marks. To do this we use the strip method:
string = 'Hello World! Lets remove the exclamation mark!'
# Replace any exclamation mark or period at the end of the string
string_1 = string.strip('.!')
print(string_1)
This gives back Hello World! Lets remove the exclamation mark . You will notice that the string still has an exclamation mark after the Hello World because the strip method only gets rid of characters
at the end of a string. Both of these methods tend to be very useful for removing html tags from text
Edit Me on GitHub!
What does end =' in Python?
The end parameter in the print function is used to add any string. At the end of the output of the print statement in python. By default, the print function ends with a newline. Passing the whitespace to the end parameter (end=' ') indicates that the end character has to be identified by whitespace and not a newline.
What does \t mean in Python?
In Python strings, the backslash "\" is a special character, also called the "escape" character. It is used in representing certain whitespace characters: "\t" is a tab, "\n" is a newline, and "\r" is a carriage return.
|