TIME NOW CALENDAR CONVERT LENGTH CONVERT TEMPERATURE DICTIONARIES, LISTS SCIENCE EDUCATION RELIGION WORK CALCULATOR CALCULATE LIFE

Python 3 programming language, learn python, tutorial

ALL TOPICS

Python 3. Strings and methods

1. Repeated strings:
a = 'Help' + 'A ' * 5
b = 'Wool' + a * 2

print(b)

Result:
WoolHelpA A A A A HelpA A A A A

2. strip method, removes spaces by default:
a = '    str    ' 'ing'
b =  a.strip() + 'ING'
print(b)

a = '00000str00000' 'ing'
b =  a.strip("0") + 'ING'
print(b)

Result:
str    ingING
str00000ingING

3. Substrings:
a = "Hello python"

print (a[4])
print (a[0:2])
print (a[2:4])

print (a[:2])    # The first two characters
print (a[2:])    # Everything except the first two characters
print (a[1:100])
print (a[20:])
print (a[2:1])

print ('\n # CONTATENTION # \n')

print ('x' + a[1:])
print ('Splat' + a[4])
print (a[:2] + a[2:])
print (a[:3] + a[3:])

print ('\n # NEGATIVE # \n')

print (a[-1])     # The last character
print (a[-2])     # The last-but-one character
print (a[-2:])    # The last two characters
print (a[:-2])    # Everything except the last two characters
print (a[-0])     # (since -0 equals 0)
print (a[-100:])  # The Full string

Result:
o
He
ll
He
llo python
ello python

# CONTATENTION #

xello python
Splato
Hello python
Hello python

# NEGATIVE #

n
o
on
Hello pyth
H
Hello python

4. Errors.

a = "Hello python"

print(a[-20])

Result:
Traceback (most recent call last):
File "hello.py", line 3, in <module>
print(a[-20])
IndexError: string index out of range

a = "Hello python"
a[0] = "Y"

print(a)

Result:
Traceback (most recent call last):
File "hello.py", line 2, in <module>
a[0] = "Y"
TypeError: 'str' object does not support item assignment

5. Length:
a = "Hello python"
print(len(a))

Result:
12

6. UTF. Try this example:
# to text
print( 'Hello\u0020World !' )

# to unicode codes
print("Šiaudas".encode('utf-8'))

# No effect
print (u'Hello, world!')

7. Replace:
str = "My string is : meter centimeter, milimeter, kilometer ";
print (str.replace("is", "was"))
print (str.replace("meter", "gram", 3))

8. Split and Replace
string = "\"Jonaitis\";\"Jonas\";"

list = string.split(';', 1 )

fname =  str(list[0])
sname =  str(list[1])

fname = fname.replace("\"", "")
fname = fname.replace(";", "")

sname = sname.replace("\"", "")
sname = sname.replace(";", "")

#print (list)

print (sname.ljust(15)  + fname + "\n")

9. String to list (array):
S="Meter"
L=list(S)
print(L)

10. remove non alphanueric characters: strip, rstrip, lstrip. Check alpha or digit characters: isalpha, isnumeric. Convert string: upper, lower, title.
S="Meter centimeter milimeter\n"
line=S.rstrip().split(" ")
print(line)
print (S.isalpha())
print (S.isdigit())
print (S.upper())
print (S.lower())

# Title words
str = "title words. this is string example....wow!!!";
print (str.title())

# Swap case
str = "Swap case. this is string example....wow!!!";
print (str.swapcase())

L="123"
print (L.isalpha())
print (L.isdigit())

L="abc"
print (L.isalpha())
print (L.isdigit())

11. Replace
strs = "abc&def#ghi"
print (strs.replace('&', '\&').replace('#', '\#'))

12. find method:
s = "On the other hand, you have a hand."
print (s. find("hand"))

# find from to
str = "On the other hands, you have a hands."

print (str.find('s',11))
print (str.find('s',11,14))

Result:
13
17
-1

ALL TOPICS