Read an Integer . For All Non-negative Integers , Print . See the Sample for Details.

Python: n (non-negative integer) copies of the first two characters of a given string

Python Basic: Practise-23 with Solution

Write a Python program to go the north (non-negative integer) copies of the outset 2 characters of a given string. Return the due north copies of the whole string if the length is less than 2.

Pictorial Presentation:

n (non-negative integer) copies of the first 2 characters of a given string

Sample Solution:-

Python Code:

          def substring_copy(str, n):   flen = 2   if flen > len(str):     flen = len(str)   substr = str[:flen]      result = ""   for i in range(n):     outcome = result + substr   render issue print(substring_copy('abcdef', 2)) print(substring_copy('p', 3));                  

Sample Output:

abab                                                                                                           ppp        

Flowchart:

Flowchart: n (non-negative integer) copies of the first 2 characters of a given string.

Visualize Python lawmaking execution:

The following tool visualize what the computer is doing step-by-step as information technology executes the said programme:

Python Code Editor:

Have another manner to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Python plan to count the number four in a given list.
Adjacent: Write a Python program to test whether a passed letter is a vowel or not.

Python: Tips of the Day

Dictionary / Set comprehensions:

Example:

test_Dict = {i: i * i for i in range(ten)}  test_Set = {i * ii for i in range(10)}  print(test_Set) impress(test_Dict)        

Output:

{0, two, iv, 6, 8, 10, 12, 14, xvi, 18} {0: 0, one: i, 2: four, three: 9, 4: sixteen, 5: 25, 6: 36, 7: 49, 8: 64, ix: 81}        

pennslitione.blogspot.com

Source: https://www.w3resource.com/python-exercises/python-basic-exercise-23.php

0 Response to "Read an Integer . For All Non-negative Integers , Print . See the Sample for Details."

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel