title-img


Mutations Python

We have seen that lists are mutable (they can be changed), and tuples are immutable (they cannot be changed). Let's try to understand this with an example. You are given an immutable string, and you want to make changes to it. Example: >>> string = "abracadabra" You can access an index by: >>> print string[5] a What if you would like to assign a value? >>> string[5] = 'k' Traceback (most recent call last): File "<stdin>

View Solution →

Find a string Python

In this challenge, the user enters a string and a substring. You have to print the number of times that the substring occurs in the given string. String traversal will take place from left to right, not from right to left. NOTE: String letters are case-sensitive. Input Format: The first line of input contains the original string. The next line contains the substring. Constraints: 1<=len(string)<=200 Each character in the string is an ascii character. Output Format: Outp

View Solution →

String Validators Python

Python has built-in string validation methods for basic data. It can check if a string is composed of alphabetical characters, alphanumeric characters, digits, etc. str.isalnum() This method checks if all the characters of a string are alphanumeric (a-z, A-Z and 0-9). >>> print 'ab123'.isalnum() True >>> print 'ab123#'.isalnum() False str.isalpha() This method checks if all the characters of a string are alphabetical (a-z and A-Z). >>> print 'abcD'.isalpha() True >>> print 'ab

View Solution →

Text Allignment Python

In Python, a string of text can be aligned left, right and center. .ljust(width) This method returns a left aligned string of length width. >>> width = 20 >>> print 'HackerRank'.ljust(width,'-') HackerRank---------- .center(width) This method returns a centered string of length width. >>> width = 20 >>> print 'HackerRank'.center(width,'-') -----HackerRank---- - .rjust(width) This method returns a right aligned string of length width. >>> width = 20 >>> print 'Hac

View Solution →

Text Wrap Python

You are given a string S and width w. Your task is to wrap the string into a paragraph of width w. Input Format: The first line contains a string, S. The second line contains the width, w. Constraints: 0<len(S)<1000 0<w<len(S) Output Format: Print the text wrapped paragraph.

View Solution →