Monday 21 March 2022

Python#13 [Strings]

STRINGS

Prain Teaser 1:

import re
def clean_strings(strings):
result = []
print(len(strings))
for value in strings:
print(value)
value = value.strip()
value = re.sub('[!#?]', '', value)
value = value.title()
result.append(value)
return result

names = ['Rama ',' Laksmana',' Sita ']

print(clean_strings(names))

Result:

3
Rama
['Rama']

It is wrong out put. can you correct the code to get the out put as below. 

3
Rama
Laksmana
Sita
['Rama', 'Laksmana', 'Sita']


Other Useful Python String Operations 
#String

s1 = 'hello'
s2 = "Hello"
print(s1, s2) # hello Hello

docstr = """
This is multi line strings. Strings can be declared within \n Single quote \'
or Double Quote\" \n Note \ is used as escape character for quote.\n
String is an array of charecters just like in any other language.
"""
print(docstr)

hello = "Hello!"
for i in hello:
print(i) # To print character by character

# String functions


str = ' Mother, is superior, god '
print(len(str)) # 22
print('is' in str) # True
print('is' not in str) # False

print('was' in str) # False
print('was' not in str) # True

if 'was' not in str:
print('Was not present in Str')

# Slicing
print(str[:6]) # Mother : for from Beginning
print(str[0:6]) # Mother. Note 0 inclusive 6 exclusive
print(str[10:]) # T superior god : for up to end
print(str[-3:]) # God using negative indexing
print(str.upper()) # MOTHER IS SUPERIOR GOD
print(str.lower()) # mother is superior god
print(str.strip()) # Mother is superior god without spaces in the front and the end
print(str.replace("Mother", "Wife")) # Check Wife is superior God ...True!!!
print(str.split('.')) # [' Mother, is superior, god ']

s1 = 'Russia '
s2 = ' Ukraine'
s3 = 'x'
s4 = s1 + s3 + s2
print(s4) # Russia x Ukraine String Concatenation using +

# Formatting
lover = 'Juliat'
str = 'I am Romeo, i love\'o love {}'
print(str.format(lover)) # I am Romeo, i love'o love Juliat

times = 1000
str = 'I am Romeo, i love\'o love {} {} times' # many place holders {}
print(str.format(lover, times)) # I am Romeo, i love'o love Juliat 1000 times

str = 'I am Romeo, i love\'o love {1} times {0} ' # using Positional indexes
print(str.format(lover, times)) # I am Romeo, i love'o love 1000 times Juliat

str = "i love seetha ..mmm, \n also i love her sister <careless whisper>"
print(str) # using \n to new line : called as escape characters

print(str.title()) # I Love Seetha ..Mmm <CR> Also I Love Her Sister <Careless Whisper> Note capitals
print(str.split(',')) # ['i love seetha ..mmm', ' \nalso i love her sister <careless whisper>']
print("123".zfill(5)) #00123

str = 'sivaji vayile zilabee'
print(str[::-1]) # Reversal : # eebaliz eliyav ijavis

str1 = 'I am alive'
print(str1) # I am alive
del(str1)
#print(str1) # Name Error str1 is not defined

# String Alignment
str1 = "|{:<10}|{:^10}|{:>20}|".format('AMET', 'ODL', 'FOR E-Learning!')
# < for Left, ^ for Centre and > for Right
print("\nLeft, center and right alignment with Formatting: ")
print(str1) # print as below
"""#Left, center and right alignment with Formatting:
|AMET | ODL | FOR E-Learning!|"""
str2 = "\n{0:^12} was founded in {1:<4}!".format("AMET ODL FOR E-Learning!", 2022)
print('\n'+str1+'\n'+ str2)
"""
|AMET | ODL | FOR E-Learning!|

AMET ODL FOR E-Learning! was founded in 2022!
"""
In the above example various string operations are explained with the respective outputs as # comments

Happy Stringing in Python with AMET ODL ☝☝☝

Bonus :
{ for E-lite learners}
Jupyter Basics

You have to start with 

$ jupyter notebook

or

$ jupyter notebook --no--browser

Features ( Try these in Ipython)

  1. Tab Completion
  2. Introspection
  3. ? to display doc string
  4. %cmd anyScript.py where cmd = run. load etc.
  5. keyboard shortcuts like Unix bash shell
  6. Magic commands
    1. special commands not built in to python itself are known as magic commands
    2. Any command prefixed by %
    3. Eg. 
      1. aArray = np.random.randm(100,100)
      2. %timeit np.dot(aArray, aArray)
    4. %debug?
    5. %pwd
    6. %matplotlib inline
    7. eg: a ='apple'
      1. a <tab>
    8. duck typing

No comments:

Post a Comment

Making Prompts for Profile Web Site

  Prompt: Can you create prompt to craft better draft in a given topic. Response: Sure! Could you please specify the topic for which you...