Python string index, access, length, slice, split, loop, strip, replace, concatenate examples.
- Define string & indexing elements
- Length of string, concatenate strings
- Repeat, return strings from functions
- Finding offset of a string
- Splitting, looping, stripping, slicing
- Replace,numeric,case functions
- in, == operator, Exercises, solutions
Learn python string defining, indexing, accessing elements, length, split, loop, strip, replace, slice, concatenate, repeat, finding offset, check digit or alphabet, lower to upper case and vice-versa, strings exercises and solutions.
Subscribe here to earn a certificate from our online courses listed here. Your low cost subscription will enable complete access to all technical articles, online tests, projects to clear all online course requirements.
Define string & indexing elements
Strings in Python are identified as a contiguous set of characters in between quotation marks. Python allows for either pairs of single or double quotes for strings.
We can access string elements using index. The indexing starts from 0 i.e. index 0 will print the first character of the string, 1 will print the second character of the string and so on.
Let's write a function to demonstrate these features of string as in program below.
S = 'spam'
print ('single quote string is',S)
S = "spam"
print ('double quote string is',S)
res = main()
single quote string is spam
double quote string is spam
Length of string, concatenate strings
Length of a string in python can be determined using len library function. The len function calculates the length of the string starting from 1 where as string indexing starts from 0. Also we can concatenate multiple strings together using '+' to make another string. This is demonstrated in the below program. In this program when we called the main function we could concatenate all strings together to make the resultant string.
string = 'Hello'
res = main(string)
print('length of string ' + string + 'is: ' + str(res))
length of string Hello is: 5
Repeat, return multiple strings from function
In python you can repeat a string just by multiplying a number to the string. Also we can return multiple strings from a function. This is demonstrated in the below program.
stri = stri*3
mystr = '='*3
mystring = "Hello"
res1,res2 = strRepeating(mystring)
Finding offset of a string
To Read Full Article
You need to subscribe & logged in
Want to contribute a new article? Write and upload your article information here