Strings in python

What is a String?

string is a sequence of characters enclosed in single quotes( ‘ ‘ ) or double quotes ( ” ” ).there is no separate data type for a single character in python,in python single character is also called as a string,example is given below

name="code skulls"
Strings in java

purple color readings are index numbers,strings always starts with 0th index number. Space comma ( , ) every thing inside quotes ( ” ” ) are considered and has its own index value

we declared a variable and stored a string “code skulls” ,so now we will access each element in the string using its index value

name[0] #c
name[7] #u
name[4] #(nothing but space)

there are two ways to access characters in the string,one is index numbers starting from zero,other is -1 is and subtracting -1 for each element from rear end

reverse index in python strings
name[-1] #s

counting length of string

there is a function called len(),which returns the length of string

len(name) #10

suppose we need double quotes inside the string like the example give below

messasge= Lisa told "get out of my house"

if you execute the above line. python will throw an error ,we should put the whole line again in double quotes

message= " Lisa told "get out of my house" "

Concatenation of strings

joining of strings is know as string concatenation ,we use plus ( + ) operator to joiin two strings ,example is given below

y="hello" + "john"

output

hello john

if we need concatenate string and number ,we need to use double quotes for the number

x= "my " + "roll number is" + 32

the above code gives error. Know as

TypeError: Can't convert 'int' object to str implicitly

we should replace ,the number with double quotes or we can use str( ) function to covert number to string

x= "my " + "roll number is" + "32"
x= "my " + "roll number is" + str(32)

String Repetition Operator (*)

if we need to repeat a string multiple times,we should use asterisk ( * ).

print "hello" * 50
'hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello '

existence of a character or a sub-string in a string:

we use keyword in to check the sub string in the string

"love" in "i love you" #true

Slicing Strings

slicing strings is also know as dividing the main string into sub strings. We can make sub strings by providing two attributes starting index ending index,syntax is given below

strt[ strating_index: ending_index : iteration]

we see some examples,but first we will declare a string

string = "codeskulls"

if we print string directly,we will get codeskulls as an output

string[:4] #'code'

the above lines returns ‘code’ as output because we left empty for the starting index ,so by default it starts from 0 and ending index is 4

string[4:] #'skulls'

here the ending index is empty and the starting index is 4 ,so we got the above output

string[1:9:2] #'oekl'

here,in the above example we provided an iteration number as 2,so this string will be divided for every 2nd index number ,calculated from the give initial index value,so the output was ‘oekl’

Last updated on by vishal devxo