Strings…
Hey coders, today you all will be very happy as we are done with our patterns and so now we will look into the concept of strings and some of the basics program regarding it. But before going into the strings, let us just give the finishing touch to our patterns by solving the last pattern. The problem which I gave you in the last blog was to print the pattern, only if the input is odd and print the error message if even input is given.

So to write the code for left hand side pattern, we have to first check our input value, n. If our n is even, print an error message otherwise just print the pattern. For printing the pattern, we have to carefully see the pattern, we can easily figure out that left diagnol elements have positions as (i,j), where both i and j are same. For right diagnol, condition is (i+j==n-1), for all the other values space should be printed. So our code will now look like this:


I discussed the above pattern with you in this strings part because, a similar problem in strings was asked by one of the leading companies, which I will be sharing with you. So let us now jump into the strings concept, we know strings is basically collection of characters. There are different number of operations which can be done with strings. Whenever we have to take an integer input, we used to write int before the input function. If we are not writing anything, by default it means our input is a string. Eg…
k=input(“Enter your name”)
If we just write, print(k), it will print our string. So using strings is pretty simple in python. There are various inbuilt methods which we can use to check our strings or to perform different operations on them.
len(s) → length of our string
s.upper() → converts our string to uppercase
s.lower() → converts our string to lowercase
s.count(‘word to be found’) → it will give the number of times, word is found in our string s
s.find(‘word to be found’) → it will give starting index of word, if it is not present it will give -1 (there are different ways of using find, which we will see later)
s.replace(‘a’,’b’) → it will replace word ‘a’ in our string by word ‘b’.
str(s) → it will convert any type of data type into string type
So these are some of the inbuilt functions in python which we can use in various ways. We will now see the importance of str function, as it can easily convert any data type to string. For example, if we want to find the number of digits in a given number, we will write our code as:
def coun():
j=0
k=int(input())
while(k>0):
j=j+1;
k=int(k/10);
print(j)
We can see that it will give us the number of digits in a number. It is easy but still we had to use some logics here, but with the help of str function we can easily convert our input to a string and then find its length using inbuilt function, which will give us the answer.
k=int(input())
s=str(k)
print(len(s))
Hence both the above codes produced the same output. So you can similarly try the above given inbuilt functions, if you have any doubts you can give it as a response. Now to end today’s blog, I will share with u a string pattern problem which was asked by ZOHO which is similar to the problem we solved in the starting. It should also print the same thing in same manner, only string should come that way. The code for the problem along with output is shown below:


This code will print the output irrespective of our input. It will print the elements in diagonal format for both the even and odd length of our string. So that’s all for now, hope you all learnt something new in python today. Try some more string problems and be familiar with inbuilt functions, we will discuss about TURTLE IN PYTHON in my next blog (sounds interesting right?) It is a new concept which you all will enjoy, till then keep coding…
