Strings Manipulation -ch-02

Kunaal ThanikKunaal Thanik
4 min read

Concatenation :

concatenate using '+' operator

prefix = "al"
suffix = "pha"
print(prefix + suffix) # output : alpha

f-String:

helpful to print message with variable values.

syntax: f" your message {var_a} and print {var_b} "

prefix = "al"
suffix = "pha"
print(f"This is prefix: {prefix} and this is suffix: {suffix}") 
#output: This is prefix: al and this is suffix: pha

Repetition:

Repeat string x no of times using '*' operator

prefix = "al"
suffix = "pha"
print((prefix + suffix)*2) # output : alphaalpha

Indexing:

starts from 0, access element using index value

prefix = "al"
suffix = "pha"
print(prefix[0]) # output : a
print(prefix[1]) # output: h

Slicing:

  • slice(stop)

    •       blog_name = "python recall"
            sliced_string = blog_name[slice(4)]
            print(sliced_string) # output: pyth
      
  • slice(start, stop, step)

    start at given index, then stop at given index while jumping by step value.

    Step is optional parameter if don't want to jump

    •       blog_name = "python recall"
            sliced_string = blog_name[slice(2,12,2)]
            print(sliced_string) # output: to ea
      
  • [:]

    up to given index[:], while excluding given value.

    eg: index[3] , will return 0,1,2 and excludes 3

    •       blog_name = "python recall"
            sliced_string = blog_name[:3]
            print(sliced_string) # output: pyt
      
  • [-i]

    starts from last index towards 0.

    e.g: -1 is last index value of string

    •       blog_name = "python recall"
            sliced_string = blog_name[-3]
            print(sliced_string) # output: a
      
  • Reverse [::-1]

    Reverse the string and return

    •       blog_name = "python recall"
            sliced_string = blog_name[::-1]
            print(sliced_string) # output: llacer nohtyp
      

String Functions

There is huge list of string functions available but these are few we should be able to keep top of mind.

capitalize, count, endswith, find, index, isalnum, isalpha, isdecimal, isdigit, isidentifier, islower, isnumeric, isspace, istitle, isupper, join, lower, partition, replace, split, startswith, strip, title, upper, sorted

We are going to use this code to learn all these functions.

book_name = "art of intrusion Version"
version = "v02"
price = "25"
discount="3.95"
publication_house = "pyhouse"
FunctioncodeOutputNote
capitalizebook_name.capitalize()Art of intrusion versionFirst letter is capitalize
countbook_name.count(i)3
endswithbook_name.endswith('ion')TrueIf string ends with given value
findbook_name.find('ion')13if not found, return -1
indexbook_name.index('ion')13if does not exist, error
isalnumbook_name.isalnum()Falseonly a-z & numbers No-space, No-characters
isalphabook_name.isalpha()Falsea-z, A-Z, no space, no characters
isdecimalprice.isdecimal()Trueonly numbers with in 0-9 , no space, valid(254) invalid(456.32)
isdigitprice.isdigit()TrueT if all numbers(124) F(Hello123, 2.52, ' ', 1/2)
islowerbook_name.islower()FalseT/F all lower?
isnumericprice.isnumeric()Trueall #, no space/char/float
isspacebook_name.isspace()Falseall whitespace ?
istitlebook_name.istitle()Falseall string titlecase ?
isupperbook_name.isupper()Falseall char uppercase ?
join' '.join( ['hello','python'] )hello pythonjoin and return 1 string
partitionbook_name.partition('rus')('art of int', 'rus', 'ion Version')partition at given value , first occurrence'''
replacebook_name.replace('art','Art')Art of intrusion Version
startswithbook_name.startswith('ar')Truereturn if string starts with given value
stripbook_name.strip()art of intrusion Versionremove leading, trailing space
upperbook_name.upper()ART OF INTRUSION VERSIONall uppercase
titlebook_name.title()Art Of Intrusion Versionall words title case, first char capital only
sortedsorted(publication_house)['e', 'h', 'o', 'p', 's', 'u', 'y']sorted list return
sorted(publication_house, reverse=True)['y', 'u', 's', 'p', 'o', 'h', 'e']reverse sort

you may find isalnum, isalpha, isdigit confusing, just to revise.

isalnum() e.g: abc123 isalpha() e.g: abc isdigit() e.g: 123

0
Subscribe to my newsletter

Read articles from Kunaal Thanik directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Kunaal Thanik
Kunaal Thanik