Found 10476 Articles for Python

Python program to print rangoli pattern using alphabets

Arnab Chakraborty
Updated on 11-Oct-2021 12:26:42

5K+ Views

Suppose we have a number n. We have to create alphabet rangoli of n x n size. n must be within 1 and 26 and it will start from a and end at z when n is 26.So, if the input is like 5, then the output will be--------e-------- ------e-d-e------ ----e-d-c-d-e---- --e-d-c-b-c-d-e-- e-d-c-b-a-b-c-d-e --e-d-c-b-c-d-e-- ----e-d-c-d-e---- ------e-d-e------ --------e--------To solve this, we will follow these steps −for i in range n-1 to 0, decrease by 1, dofor j in range 0 to i-1, doprint "--"for j in range n-1 to i+1, decrease by 1, doprint character whose ASCII is j+97 and print ... Read More

Python program to print decimal octal hex and binary of first n numbers

Arnab Chakraborty
Updated on 11-Oct-2021 11:21:48

1K+ Views

Suppose we have a value n. We have to print Decimal, Octal, Hexadecimal and Binary equivalent of first n numbers (1 to n) in four different columns. As we know, we can express the numbers with prefix characters d, o, X and b for decimal, octal, hexadecimal and decimal respectively.So, if the input is like n = 10, then the output will be1    1    1    1 2    2    2   10 3    3    3   11 4    4    4  100 5    5    5  101 6    6    6 ... Read More

Python program to print design door mat texture using characters

Arnab Chakraborty
Updated on 11-Oct-2021 11:16:15

630 Views

Suppose we have two numbers n and m, m will be multiple of n. We have to draw a door mat pattern with a word say "WELCOME" in the middle. The mat size will be n x m. We have to make this mat using dots(.), hyphens (-), pipe symbols (|) and the text at middle.So, if the input is like n = 5 m = 15, then the output will be------.|.------ ---.|..|..|.--- ----WELCOME---- ---.|..|..|.--- ------.|.------To solve this, we will follow these steps −for i in range 1 to n-1, increase by 2, doprint(integer of ((m-i*3)/2) number of '-', then ... Read More

Python program to wrap a text into paragraph with width w

Arnab Chakraborty
Updated on 11-Oct-2021 11:11:31

974 Views

Suppose we have a string s and width w. We have to wrap this text into a paragraph with width w. This can be done very easily with fill() function present inside textwrap library. So we have to import textwrap library first.So, if the input is like s = "The quick brown fox jumps over the lazy dog" w = 9, then the output will beThe quickbrown foxjumpsover thelazy dogTo solve this, we will follow these steps −take the string into stake width into wcall textwrap.fill(s, w) by passing s as the first argument, and w as the second argumentExampleLet ... Read More

Python program to show diamond pattern with 2n-1 lines

Arnab Chakraborty
Updated on 11-Oct-2021 11:07:52

2K+ Views

Suppose we have a number n. We have to draw a diamond pattern with asterisks with 2n-1 lines. First 1 to n lines contain 1 to n number of asterisks, and next they are decreasing from n-1 to 1.So, if the input is like n = 5, then the output will be    *    * *   * * *  * * * * * * * * *  * * * * * * * * * *To solve this, we will follow these steps −for i in range ... Read More

Python program to validate string has few selected type of characters or not

Arnab Chakraborty
Updated on 11-Oct-2021 11:01:37

134 Views

Suppose we have a string s. We have to check whether the string contains the following or not.NumbersLowercase lettersUppercase lettersNote − There may be some other symbols, but these three must be thereSo, if the input is like s = "p25KDs", then the output will be TrueTo solve this, we will follow these steps −arr := an array of size 3 and fill with Falsefor each character c in s, doif c is alphanumeric, thenarr[0] := Trueif c is in lowercase, thenarr[1] := Trueif c is in uppercase, thenarr[2] := Truereturn true when all items of arr are trueExampleLet us ... Read More

Python program to count number of substring present in string

Arnab Chakraborty
Updated on 11-Oct-2021 10:58:07

729 Views

Suppose we have a string s and a substring t. We have to count how many times t occurs in s.So, if the input is like s = "abaabcaabababaab", t = "aab", then the output will be 3 because the substrings are ab(aab)c(aab)abab(aab).To solve this, we will follow these steps −cnt := 0for i in range 0 to (size of s - size of t), doif substring of s[from index i to i + size of t - 1] is same as t, thencnt := cnt + 1return cntExampleLet us see the following implementation to get better understandingdef solve(s, t): ... Read More

Python Program to calculate function from indicator random variable from given condition

Arnab Chakraborty
Updated on 11-Oct-2021 11:22:04

326 Views

Suppose we have two values k and n. Consider a random permutation say p1, p2, ..., pn of first n natural numbers numbers 1, 2, ..., n and calculate the value F, such that F = (X2+...+Xn-1)k, where Xi is an indicator random variable, which is 1 when one of the following two conditions holds: pi-1 < pi > pi+1 or pi-1 > pi < pi+1 and Xi is 0 otherwise. We have to find the expected value of F.So, if the input is like k = 1 n = 1000, then the output will be 1996/3To solve this, we ... Read More

Python program to change character of a string using given index

Arnab Chakraborty
Updated on 11-Oct-2021 10:55:39

380 Views

Suppose we have a string s, an index i and a character c. We have to replace the ith character of s using c. Now in Python, strings are immutable in nature. We cannot write a statement like s[i] = c, it will raise an error [TypeError: 'str' object does not support item assignment]So, if the input is like s = "python", i = 3, c = 'P', then the output will be "pytPon"To solve this, we will follow these steps −left := s[from index 0 to i]right := s[from index i+1 to end]return left concatenate c concatenate rightExampleLet us ... Read More

Python program to split a string and join with comma

Arnab Chakraborty
Updated on 11-Oct-2021 10:31:58

1K+ Views

Suppose we have few words that are separated by spaces. We have to split these words to form a list, then join them into a string by placing comma in-between.So, if the input is like s = "Programming Python Language Easy Funny", then the output will be Programming, Python, Language, Easy, FunnyTo solve this, we will follow these steps −words := a list of words by applying split function on s with delimiter " " blank space.ret := join each items present in words and place ", " in between each pair of wordsreturn retExampleLet us see the following implementation ... Read More

Advertisements