String in Python

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 1

// Strings //

1. Strings are sequence of characters enclosed within single, double or


triple quotes.
' '
" "
''' '''
eg: my_string = "Hello world"
first index = 0 my_string[0] // 'H'
last index = -1 my_string[-1] // 'd'
my_string[0:5] // Hello
my_string[0:5:2] // skip 1 letter Hlo
my_string[1:] // ello world

2. length of string = len(my_string)


3. string to lowercase = my_string.lower()
4. string to uppercase = my_string.upper()
5. replace a substring = my_string.replace('H', 'F')
6. number of occurences of substring = my_string.count("l") # return 3
=> it will count the substring repeated by no of times (l -> 3)
7. finding index of substring = my_string.find("world"); # 6

8. splitting a string = fruit = " i like html java python"


fruit.split(' ')
[ 'i', 'like', 'html', 'java', 'python' ]
9. str ="test"
str*3
10. concatenation str+"good"
11. string_name.islower() : returns boolean value if it is in lower or upperCase
12. in or not in

You might also like