write in python
String: A sequence of characters enclosed within single or double quotes. A string can include any character, including special characters and spaces.
Examples: "Hello World!", 'I have $250 cash and a credit card.'
Note: Each character in a string falls in a specific position, so we can assign an index (numbering) to each character. Such assignment helps us to perform mathematical operations on strings.
Operations:
* finding characters by position, length of string, concatenating, replicating, slicing
* Back slashes for escape sequences: new line, tab
* String methods: upper, replace, substrings, raw strings
It is a basic program for hands on String methods in python, indexing, slicing, Upper(), raw string and other methods are commonly used everywhere and these are quite easy to do and understand and thing is you need to practice it to learn it quickly.
Nothing much to explain in this question, as it is just straight forward usage of string methods, i have written the python program with enough comments against each line of my code, which will give you clear view on how to syntax and write each string method and then what will be its corresponding output.
Now come to the code:
CODE:
# String declaration str1 = "Hello World," str2 = 'I have $250 cash and two credit cards' # index = 0, gives the first character, # index = 4, gives the fifth character as indexing starts from 0,1,2,3,4... and so on # index = -1 gives the last character # finding character of str1 using index = 0, 4, -1 print("first string = ", str1) print(str1[0]) print(str1[4]) print(str1[-1]) # finding character of str2 using index = 0, 4, -1 print("second string = ", str2) print(str2[0]) print(str2[4]) print(str2[-1]) # len() function is used to find the lenth of the string length_of_str1 = len(str1) print("length of first string :",length_of_str1) length_of_str2 = len(str2) print("length of second string :",length_of_str2) str3 = "Apple" print("length of string 'Apple': ",len(str3)) # Concatenation of strings in python can be simply achieved by using + sign concatenated_string = str1 + str2 print("After Concatenation : ",concatenated_string) # for replicating a string n times , just multiply string with n using * sign as follows str4 = "abc" print("repeat/replicate 'abc' 4 times : ", str4 * 4) # to print first few , last few or one particular index to other index, taking a slice/piece of string, we use slicing # printing first 3 letters print("first 3 letters of first string: " ,str1[0:3]) print("first 3 letters of second string: " ,str2[0:3]) # printing first to last(whole string) character of string using slice print(str1[0:-1]) print(str2[0:-1]) # for tab we use '\t' for new line we use '\n' print("Number of tabs used here is \t\t\t\t 4") # 4 will be printed after 4 tab spaces print("Using new line char, print number in new line \n 445") # 445 is printed in new line # Upper() is used to capitalize the small letters in the string new_string = "Tomato" print("using upper for 'Tomato' :", new_string.upper()) # replace() is used to replace a character in string with another string new_string2 = "Power" print("Using replace() to replace P with L in word 'Power' : ", new_string2.replace('P','L')) # for checking a string is a "substring" of a given string or not we simply use 'in' print('butter' in 'butterfly') # if first one contains in second, it prints True print('pain' in 'spain') # pain is substring of spain so True print('red' in 'madrid') # prints false as, red not a substring of madrid # raw String , it removes the effect of special characters like escape/tab/new line chars '\n' ,'\t' # we have to prefix r or R just before '\n' or '\t' to treat it as raw string and not special escape chars print("Printing with special char "+ "\t" + "\t" +"4") # \t treated as special tab character print("Printing with special char "+ r"\t" + r"\t" +"4") # \t treated as normal character
OUTPUT :
Thank you...!!!
write in python String: A sequence of characters enclosed within single or double quotes