|
| 1 | +# Diving into string type data type |
| 2 | +name = "CODEX" |
| 3 | +print (name) # prints the whole string |
| 4 | +print (name[1]) # prints the character os specific index, here "O" |
| 5 | +print (name[1:3]) # prints the string from 1st index to 3rd i.e, 2nd character to 4th |
| 6 | +print (name[1:]) # prints the whole string from 1st index i.e, 2nd character to last |
| 7 | +print (name*2) # prints the string 2 times |
| 8 | +print (name + " ITER") # prints the string along with the added string |
| 9 | +print (name[::-1]) # prints the string in reversed order |
| 10 | +print ("The String is",name,"ITER") #printing the string along with other string |
| 11 | + |
| 12 | +# printing sentences |
| 13 | +# '\t' : to give tabs |
| 14 | +# '\n' : next line |
| 15 | +long_string = "\n\nLorem \t ipsum \t dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. \n Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.\n Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum." |
| 16 | +print (long_string) |
| 17 | + |
| 18 | +#another way to write long paragraphs in multi line |
| 19 | +long_string = """\n\nLorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. |
| 20 | +Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. |
| 21 | +Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.\n\n""" |
| 22 | +print (long_string) |
| 23 | + |
| 24 | + |
| 25 | +# some other useful built-in string methods |
| 26 | +print (name.capitalize()) # capitalizes first letter of string |
| 27 | +print (name.upper()) # converts all lowercase letters in string to uppercase |
| 28 | +print (name.lower()) # converts all uppercase letters in string to lowercase |
| 29 | +print (len(name)) # prints the length of the string |
| 30 | + |
0 commit comments