Skip to main content

Python String.

 Innomatics Research Labs 


Im a innomatics student, this is for innomatics research labs notes (the best institute of hyderabad) once search in google, course = data science and full stack development


Innomatics Research Labs is a pioneer in “Transforming Career and Lives” of individuals in the Digital Space by catering advanced training on IBM Certified Data Science, Python, IBM Certified Predictive Analytics Modeler, Machine Learning, Artificial Intelligence (AI), Full-stack web development, Amazon Web Services (AWS), DevOps, Microsoft Azure, Big data Analytics, Digital Marketing, and Career Launching program for students who are willing to showcase their skills in the competitive job market with valuable credentials, and also can complete courses with a certificate.


Strings:


  • strings are a sequence or a char, enclosed in qoutes.




creating a string

  • a string can be created by encloing a char or sequence of chars in single qoutes or double qoutes or triple single qoutes or triple double qoutes

  • we use multi-line stings for documentation purpose; doc_strings


# creating a string
string_1 = 'this a string enclosed in single qoutes'

string_2 = "this a string enclosed in double qoutes"

string_3 = '''This is a multiple line
              string, which has
              3 lines in it and is enclosed with triple single qoutes'''

string_4 = """This is a multiple line
              string, which has
              3 lines in it and is enclosed with triple double qoutes"""




strings can be indexed.

  • since strings are a sequence of chars, and this seq has an order. We can access individgual chars by their index or position in that seq enclose in [].

    x = 'python'

    string x has a len of 6, (len of string is equals to the number of chars in that string) the first index is 0 (indexing in python starts with 0)



Ex ;

x = 'python'

print('len of sting x is ',len(x))

print(x[0]) # indexing

Output ; 

len of sting x is  6
p

reverse indexing

  • we can also access the chars in a string using reverse indexing.
  • reverse indexing starts at the end of the string, reverse indexing starts with -1

Ex ; 

x = 'Python'
print(x[-1])

Output ;

n

  • positive index starts with 0 and ends at len(string)-1
  • negative index starts with -1 and ends at -len(string
  • giving a number which doesn't fall in the given range will result in IndexError

slicing

  • as the word suggest, we can get a sub-string of the given string by performing slicing

  • to slice a string, we need three values: start_indexend_indexstep_size

    • start_index: from where the slicing has to start, or the starting index of sub-string.
    • end_index: in slicing, end index is end index - 1. end_index tells till which index the slicing has to happen, (we are not going to include char at end_index)
    • step_size: it talks about how many steps it has to move while performing the slicing. by default, step_size is +1
  • syntax for slicing:

    • var[start_index: end_index: step_size]

Ex ;

x = 'Python is a great language to learn, it spoken by a large population of coders'

print(x[0:8])
print(x[0:8:2])

Output ;

Python i
Pto 

print(x[::-1]) # reversing a string using -1 as step size

sredoc fo noitalupop egral a yb nekops ti ,nrael ot egaugnal taerg a si nohtyP

  • while using a negative step size, we travel from end to start.
  • the start and end index values also follow this.
  • while using -ve step size, the default start and end indicies are (-1 or len(string)-1) and (-len(string) or 0) respectively

Ex ; 

x[5:15:-2] # you get a empty string as the direction and the indecies don't match

Output ; ''

string concatenation


Ex ;

a, b = 'hi', 'everyone'
c = a+' '+b
c

Output ;

'hi everyone'

membership operations on strings


Ex ; 

y = 'Python'

print( y in x)
print(y[::-1] not in x)

Output ;

True
True

Methods in strings


  • when we assign a string to a variable, we are basically performing an object initialization. By doing this, we can invoke or use the methods that are there in str class.
  • string methods are inbuilt prewritten functions that acts on the given string to get a desired output.
  • results we get from string methods has to be saved in another variable to access them later as strings are immutable and these results have a different memory location than the given string.

print(dir(y))      =       ['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isascii', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']

Ex ; 

print(type(x))

output ; 

<class 'str'>

upper()

  • upper() is goint to convert lowercase alphabets in given string to uppercase.

Ex ;

x = 'abc'
print(x.upper())
print(x)

# note: in other to use the new string or output of the string methods, we need assign or reassign to variable
print()
x = x.upper()
print(x)

Output ; 

ABC
abc

ABC

lower()

  • lower() converts upper_case alphabets to lower_case

Ex ; 

print(x)

x = x.lower()
print(x)

Output ; 

ABC
abc

capitalize()

  • if an alphabet is at 0th index, it is going convert it to capital case

Ex ;

x = 'abc def'
print(x)
x = x.capitalize()
print(x)
print()

x = '*abc def'
print(x)
x = x.capitalize()
print(x)
print()

x = '''a
       b
       c'''
print(x)
x = x.capitalize()
print(x)
print()

Output ;

abc def
Abc def

*abc def
*abc def

a
       b
       c
A
       b
       c

title()

  • title() going to convert every first alphabet of every consecutive word separeted with a space to capital case.

Ex ;

x = 'momma, i killed a man!'
print(x)
x = x.title()
print(x)

print()

x = '''a
b
c'''
print(x)
x = x.title()
print(x)

Output ; 

momma, i killed a man!
Momma, I Killed A Man!

a
b
c
A
B
C

swapcase()

  • swapcase() is going swap the cases of alphabets

Ex ;

x = 'AbCd EfGh'
print(x)
x=x.swapcase()
print(x)

Output ;

AbCd EfGh
aBcD eFgH

count()

  • it will return the value of occurences of given sub-string in a string.

Ex ;

x = """Betty Botter bought some butter
But she said the butter’s bitter
If I put it in my batter, it will make my batter bitter
But a bit of better butter will make my batter better
So ‘twas better Betty Botter bought a bit of better butter"""

x = x.lower()

print(x.count('b'))

print(x.count('tt'))

Output ;

23
17

startswith() and endswith()

  • these methods will return a boolean output.
  • True, when the char or sub-string is passed is at the start and end of the string respectively
  • False, otherwise

Ex ;

x = 'these'
print(x)
print()

# startswith()
print(x.startswith('t'), x.startswith('the'), x.startswith('e'))

#endswith()
print(x.endswith('e'), x.endswith('ese'), x.endswith('t'))

Output ;

these

True True False
True True False

find and index

  • both methods will return the index position of first occurence of given char or sub-string.

  • find and index performs same task, however they behave differently when a char or sub-string which is not present in the parent string is given as an input/arguement.

    • find will return -1
    • index will return an error


Ex ; 

x = """Betty Botter bought some butter
But she said the butter’s bitter
If I put it in my batter, it will make my batter bitter
But a bit of better butter will make my batter better
So ‘twas better Betty Botter bought a bit of better butter"""

x = x.lower()

print(x.find('m'), x.find('was'), x.find('zz'))
print()
print(x.index('m'), x.index('was'),)
print(x.index('zz'))

Output ;

22 180 -1

22 180

rfind and rindex

  • both methods will return the index position of first occurence of given char or sub-string from the right side of the string.

  • rfind and rindex performs same task, however they behave differently when a char or sub-string which is not present in the parent string is given as an input/arguement.

    • rfind will return -1
    • rindex will return an error

Ex ;

x = """Betty Botter bought some butter
But she said the butter’s bitter
If I put it in my batter, it will make my batter bitter
But a bit of better butter will make my batter better
So ‘twas better Betty Botter bought a bit of better butter"""

x = x.lower()

print(x.rfind('m'), x.rfind('was'), x.rfind('zz'))
print()
print(x.rindex('m'), x.rindex('was'),)
print(x.rindex('zz'))

Output ;

158 180 -1

158 180

replace()

  • replaces every occurence of given char or sub-string with new char or sub-string

Ex ;

y = 'abc abc abc'
print(y)
print(y.replace('a', 'A'))
print(y.replace('bc', 'BC'))
print(y)

Output ;

abc abc abc
Abc Abc Abc
aBC aBC aBC
abc abc abc

split()

  • split() returns a list of sub-strings which were broken/splitted on given char or sub-string, if no arg/input is provide, it'll split on space.

Ex ;

x = 'abc def ghi'
print(x.split())
print(x.split('e'))

Output ;

['abc', 'def', 'ghi']
['abc d', 'f ghi']


partition()

  • partition() splits on the given char or sub-string.
  • it'll break the string on the given char or sub-string into three parts and returns it as a tuple.

Ex ;

x = 'abc def ghi'
print(x.partition('e'))
#print(x.partition())
print(x.partition(' '))

Output ;

('abc d', 'e', 'f ghi')
('abc', ' ', 'def ghi')

rpartition() abd rsplit()


Ex ;

print(x.rpartition('e'))
print(x.rpartition(' '))
print('\n \n')
print(x.rsplit('e'))
print(x.rsplit())

Output ;

('abc d', 'e', 'f ghi')
('abc def', ' ', 'ghi')

 

['abc d', 'f ghi']
['abc', 'def', 'ghi']

center

  • it is going to bring or readjust the given string to be at center of the width provided with spaces on either side (if no char input/arg is given) or with the given char

  • the width given as an input/arg should be greated than len(string), if not, the orginal string will be given as input


Ex ;

x = 'python'
print(x)
xCentered = x.center(20) 
print(xCentered)
len(xCentered)
print()
xCent_ast = x.center(20,'*')
print(xCent_ast)

Output ;

python
       python       

*******python*******

ljust() & rjust()

  • the width given as an input/arg should be greated than len(string), if not, the orginal string will be given as input

Ex ;

print(x)
print(x.ljust(20)) 
print(x.ljust(20,'*'))
print()

print(x.rjust(20))
print(x.rjust(20,'*'))

Output ;

python
python              
python**************

              python
**************python

zfill()

  • zfill() is used to pad or fill the remaining of given width with zeros on left side

Ex ;

print(x)
print(x.zfill(20))

Output ;

python
00000000000000python

strip()

  • it removes the whitespaces at the start and end of the string

  • if a char or a sub-string is given as input/arg, it'll removes the given char/sub-string


Ex ;

xCentered

Output ;

'       python       '

lstrip() and rstrip()

  • lstrip() is used to remove the prefix, it removes the given char/s if they are at start of the string. (space, if no char/s are provided)

  • rstrip() is used to remove the suffix, it removes the given char/s if they are at end of the string. (space, if no char/s are provided)


Ex ;

print(xCent_ast)
print(xCent_ast.lstrip('*'))
print(xCent_ast.rstrip('*'))

Output ;

*******python*******
python*******
*******python

removeprefix() and removesuffix()

  • removeprefix() and removesuffix() will remove only given char, but not the sequence of given char like in lstrip() and rstrip()

#print(xCent_ast)
#print(xCent_ast.removeprefix('*'))
#print(xCent_ast.removesuffix('*'))

join()

  • join() is going to join given sequence of chars using the string on which it is called.

Ex ;

x = 'python is great lang :/'
print(x)
print()
x1 = x.split()
print(x1)

y = ''
z = '**'
print()
print(y.join(x1))
print()
print(z.join(x1))

Output ;

python is great lang :/

['python', 'is', 'great', 'lang', ':/']

pythonisgreatlang:/

python**is**great**lang**:/

methods with boolean output


Ex ;

x = 'Python is a great lang'
y = '123'
# isalpha() --> check wether given string is a string of alphabets
print(x.isalpha())
print(x[:5].isalpha())
print(y.isalpha())
print(y.isnumeric())
z = y[:5]+x
print(z.isalnum())
print('xyf'.isidentifier())
print('^ahg'.isidentifier())

# Explore the remaining methods starting with 'is'
#:)

Output ;

False
True
False
True
False
True
False

                                          ( Innomatics Research Labs )

Comments

Popular posts from this blog

Navigating the GenAI Frontier: Transformers, GPT, and the Path to Accelerated Innovation ( LLM )

  Historical Context: Seq2Seq Paper and NMT by Joint Learning to Align & Translate Paper : * Seq2Seq model introduced in "Sequence to Sequence Learning with Neural Networks" paper by Sutskever et al., revolutionized NLP with end-to-end learning. For example, translating "Bonjour" to "Hello" without handcrafted features. * NMT by "Joint Learning to Align and Translate" (Luong et al.) improved Seq2Seq with attention mechanisms. For instance, aligning "Bonjour" and "Hello" more accurately based on context. Introduction to Transformers (Paper: Attention is all you need) : * Transformers introduced in "Attention is All You Need" paper. * Replaced RNN-based models with attention mechanisms, making them highly parallelizable and efficient. Why transformers : * Transformers capture long-range dependencies effectively. * They use self-attention to process tokens in parallel and capture global context efficiently.   ...

Language Modeling ( LLM )

Language Modeling ( LLM ) *   What is Language Modeling :   Language modeling powers modern NLP by predicting words based on context, using statistical analysis of vast text data. It's essential for tasks like word prediction and speech development, driving innovation in understanding human language. Types of language models :  N-gram Models  : These models predict the next word based on the preceding n-1 words, where n is the order of the model. For example, a trigram model (n=3) predicts the next word using the two preceding words. N-gram Language Model Example : Consider a simple corpus of text: I love hiking in the mountains. The mountains are beautiful. Trigram Model: P("in" | "hiking", "love") = Count("hiking love in") / Count("hiking love") P("are" | "mountains", "the") = Count("mountains the are") / Count("mountains the") Neural Language Models: Neural network-based language...