Learn Python Part 4 String Methods in Python A complete introduction

Veröffentlicht am: 10 Mai 2020
auf dem Kanal: Bioinformatics and Computational Biology
291
8

This Video will cover the following topic:
String Methods
Changing the capitalizations of a string
Stripping the string
Reversing a string
Split a string
Replacing the occurrence of one substring with a new substring
Testing what a string is composed of
Format values into a string
Join a list of string
Test the starting and ending of string
String Module


CODE::
#String Methods
#Changing the capitalizations of a string
#str.upper
#str.lower
#str.capitalize
#str.title
#str.swapcase

s = 'This IS a String'
print('Upper case: ', s.upper())
print('Lower case: ', s.lower())
print('capitalize case: ', s.capitalize())
print('Title case: ', s.title())
print('Swape case: ', s.swapcase())

#Stripping the string
#str.strip
s = 'a line with leading and trailing space '
print("stripped string: ", s.strip())
s = "---a python prompt"
print("Stripped string: ", s.strip('-'))

Reversing a string
s = "hello"
print(reversed(s))
print("".join(reversed(s)))

Split a string
s = "this is a string"
print(s.split(' '))
s = 'this is a sentence'
print(s.split('e', maxsplit=1))
print(s.split('e', maxsplit=2))

Replacing the occurrence of one substring with a new substring
s = "Make sure to foo your sentence."
print(s.replace('foo', 'spam'))
s = "It can foo multiple examples of foo if you want."
print(s.replace('foo', 'spam'))
s = "It can foo multiple examples of foo if you want or you can limit the foo with the third argument"
print(s.replace('foo', 'spam', 1))

#Testing what a string is composed of
#str.isalpha
s = "Hello World"
s1 = "Hello2World"
s2 = "HelloWorld!"
s3 = "HelloWorld"
print("s: ", s.isalpha())
print("s1: ", s1.isalpha())
print("s2: ", s2.isalpha())
print("s3: ", s3.isalpha())

#str.isupper
s = "hello world"
s1 = "HELLO WORLD"
print("s: ", s.isupper())
print("s1: ", s1.isupper())

#str.islower
s = "hello world"
s1 = "HELLO WORLD"
print("s: ", s.islower())
print("s1: ", s1.islower())

#str.isdigit
#str.isnumeric
#str.isdecimal
#str.isspace

#Format values into a string
i = 10
f = 1.5
s = "foo"
l = ['a', 1, 2]
d = {'a': 1, 2: 'foo'}
print("{}{}{}{}{}".format(i,f,s,l,d))
print(str.format("{}{}{}{}{}", i , f , s, l, d))
s = "I am from Australia. I love cupcakes from Australia!"
print("I am from {}. I love cupcakes from {}!".format("Australia", "Australia"))
print("I am from {0}. I love cupcakes from {0}!".format("Australia"))

#Join a list of string
l = ["once", "upon", "a", "time"]
print("----".join(l))

#Counting number of times a substring into a string
#str.count(sub[,start[,end)
s = "She sells seashells by the seashore."
print(s.count("sells"))

Test the starting and ending of string
#str.startswith
#str.endswith
s = "this is a test string"
print(s.startswith("t"))
print(s.startswith("T"))
print(s.endswith("ing"))

String Module
import string
print(string.ascii_letters)
print(string.ascii_lowercase)
print(string.ascii_uppercase)
print(string.digits)
#string.digits
#string.hexdigits
#string.octaldigits
#string.punctuations
print(string.printable)


Auf dieser Seite können Sie das Online-Video Learn Python Part 4 String Methods in Python A complete introduction mit der Dauer stunde minuten sekunde in guter Qualität ansehen, das der Benutzer Bioinformatics and Computational Biology 10 Mai 2020 hochgeladen hat, den Link mit Freunden und Bekannten teilen, dieses Video wurde auf Youtube bereits 291 Mal angesehen und es wurde von 8 den Zuschauern gefallen. Viel Spaß beim Betrachtenden Zuschauern gefallen!