Roshan Book

My Tech Notebook

Tag Archives: python strings

Python tutorial 13–Strings

Common methods :

#string.upper() – This will change everything to upper case
#’This is a string {}’.format(42) –  will publish This is a string 42
# s.capitalize()- Publish in sentence case
#s.lower()
#s.swapcase() -opposite case
# s.find(‘is’) – returns the postion
#s.replace(‘This’,’That’) – This is entirely dirreferen objetc that returns
#s.strip()- this removes white space from beginning and end
#s.rstrip() – removes white space from end of string
#s1.rstri(‘n’)- takes out only new line
#s.isalnum()-if checks it has only alphanumeric charactures
#s.isalpha()-checks only for alpha charcters, like a to z onle
#s.isdigit()- checks if only digit
#s.isprintable()- checks if all characters are printable

 

Format method

#s=”this is {},that is {}”.format(a,b)
#now you can do s.format(d,e) and this geenrates a new objetc which youc an save in a different vairable

# You can also specify as ‘this is {1} and that is {0}’.format(a,b))
# this will print this is b and that is a
# you can also use dictionary to rpint than postion like ‘this is {bob} and that is {fred}’.format(bob=a,fred=b)
# another way is to define dictionaly first d=dict(bob=a, fred=b) and then use ‘this is {bob} and that is {fred}’.format(**d)

# Splitting and joining stings
s= ‘This is string a strng of words’
#s.split() splits depending on white space i.e. [“This”,’is’,’string’,’of’,’words’
                                               
# to specify argument to splits
#words=split(‘i’)  # this gives strings with all i removed and words splitter on i character
#    joining   
# ‘,’join(words) # This will join back all the words
                           
# docs for string methods : http://docs.python.org/py3k/library/stdtypes.html#string-methods
# Python full library at http://docs.python.org/py3k/library/

Python tutorial 13–Strings

Common methods :

#string.upper() – This will change everything to upper case
#’This is a string {}’.format(42) –  will publish This is a string 42
# s.capitalize()- Publish in sentence case
#s.lower()
#s.swapcase() -opposite case
# s.find(‘is’) – returns the postion
#s.replace(‘This’,’That’) – This is entirely dirreferen objetc that returns
#s.strip()- this removes white space from beginning and end
#s.rstrip() – removes white space from end of string
#s1.rstri(‘n’)- takes out only new line
#s.isalnum()-if checks it has only alphanumeric charactures
#s.isalpha()-checks only for alpha charcters, like a to z onle
#s.isdigit()- checks if only digit
#s.isprintable()- checks if all characters are printable

 

Format method

#s=”this is {},that is {}”.format(a,b)
#now you can do s.format(d,e) and this geenrates a new objetc which youc an save in a different vairable

# You can also specify as ‘this is {1} and that is {0}’.format(a,b))
# this will print this is b and that is a
# you can also use dictionary to rpint than postion like ‘this is {bob} and that is {fred}’.format(bob=a,fred=b)
# another way is to define dictionaly first d=dict(bob=a, fred=b) and then use ‘this is {bob} and that is {fred}’.format(**d)

# Splitting and joining stings
s= ‘This is string a strng of words’
#s.split() splits depending on white space i.e. [“This”,’is’,’string’,’of’,’words’
                                               
# to specify argument to splits
#words=split(‘i’)  # this gives strings with all i removed and words splitter on i character
#    joining   
# ‘,’join(words) # This will join back all the words
                           
# docs for string methods : http://docs.python.org/py3k/library/stdtypes.html#string-methods
# Python full library at http://docs.python.org/py3k/library/

Python Tutorial 1 Strings

Programming is just writing in a language which computer understands and through that giving computer instructions to perform certain tasks

escape operator to escape a quote. E.g. “don”t” will escape double quote before t.

n are used to print text in multiple lines

t for tab

“”” is used to write multiple lines of text

Concatenation – “jhon”+”rambo”  or “jhon” “rambo”

format specifier “John %s%s” % (“Every” , “man”)

John everyman

>  >  >  “%s %s %10s” % (“John” , “Every”, “Man”)
‘John Every          Man’
>  >  >  “%-5s %s %10s” % (“John” , “Every”, “Man”)
John     Every          Man
How It Works
In the first line of code, the word  Man  appears far away from the other words; this is because in your
last format specifier, you added a 10, so it is expecting a string with ten characters. When it does not
find ten (it only finds three . . .   M – a – n) it pads space in between with seven spaces.
In the second line of code you entered, you will notice that the word  Every  is spaced differently. This
occurs for the same reason as before — only this time, it occurred to the left, instead of the right.
Whenever you right a negative ( – ) in your format specifier, the format occurs to the left of the word. If
there is just a number with no negative, it occurs to the right.

%d bloggers like this: