+
¶For string the +
operator is used for concatenation, joining multiple strings together.
word1 = 'Hello'
word2 = 'Python'
greet = word1 + ' ' + word2 + '!'
print(greet)
*
¶The *
operator is used for "multiplying" a string, repeating and concatenating it the given times.
greet3times = greet * 3
print(greet3times)
len()
¶The len()
statement returns the length of the string.
print(len(greet))
[]
¶A single charcter of a string can be access by indexing it, starting from zero:
print(greet[0])
Question: what will happen if we index with a negative number?
print(greet[-1])
Question: what will happen if we with a number larger than the length of the string?
print(greet[100])
We can also create substrings by fetching a slice of a string.
Note that the end index is exclusive, so if the slice is given as [4:6]
, then the characters with the index 4 and 5 will be sliced.
print(greet[0:5])
print(greet[6:7])
The first (start) index can be omitted, by default it will be zero:
print(greet[:5])
The second (end) index can also be omitted, by default it will be the end of the string:
print(greet[6:])
Question: what happens if we omit both the start and the end index?
print(greet[:])
Question: what happens if we use negative indices?
print(greet[-7:])
print(greet[1:-2])
Question: what happens if the end index is larger than the length of the string?
print(greet[6:100])
A comprehensive list of the built-in functions can be found in the 'string library' reference documentation.
These string functions are methods, which means they can be called on a string instance (value or variable) in a form stringvar.method(parameters)
.
They do not modify the original string, but return a new instance.
lower
¶Replace all letters to lowercase.
print(greet)
greet_lower=greet.lower()
print(greet_lower)
upper
¶Replace all letters to uppercase.
print(greet)
greet_upper=greet.upper()
print(greet_upper)
capitalize
and title
¶Replace the very first letter or the first letter of each words to uppercase. The rest will be turned to lowecase.
print(greet_lower)
greet_capital=greet_lower.capitalize()
print(greet_capital)
greet_title=greet_lower.title()
print(greet_title)
find
¶Looks up the first occurance of a character or a substring in a string. The result is the starting index position of the first occurance as an integer
. Keep in mind that the first index is 0
! The returned value is -1
if the substring was not found.
print(greet)
location = greet.find('Python')
print(location)
print(greet)
location = greet.find('java')
print(location)
The starting index of the search can also be passed to the function. This way multiple occurances of a substring can be looked up.
print(greet3times)
location = greet3times.find('Python')
print(location)
location = greet3times.find('Python', location + 1)
print(location)
This function is case-sensitive.
If you would like to search for both lower and uppercase variants, you may convert the string to lowercase first!
print(greet)
location = greet.find('python')
print(location)
print(greet.lower())
location = greet.lower().find('python')
print(location)
replace
¶Replace all occurances of a substring to another substring.
This function is also case-sensitive.
greet_alternative = greet3times.replace('Hello', 'Hi')
print(greet_alternative)
lstrip
, rstrip
, strip
¶All functions are used to trim unrequired whitespace characters (spaces, tabulators, newlines) from a string.
lstrip
- remove whitespace characters from the lefthand side.rstrip
- remove whitespace characters from the righthand side.stri
- remove whitespace characters from both sides.greet_world = ' --== Hello World ==-- '
print(greet_world.lstrip())
print(greet_world.rstrip())
print(greet_world.strip())
The characters to remove can also be specified otherwise:
print(greet_world.strip(' -='))
startswith
, endswith
¶These functions verifies whether a string starts or ends with the given substring. The result is a boolean value (True
or False
.)
This function is also case-sensitive.
print(greet.startswith('Hello'))
print(greet.startswith('Hi'))
split
¶Split a string into a list of substring by defining a so-called separator or delimiter character or string. The separator is removed from the string.
print(greet3times)
words = greet3times.split('!')
print(words)
Question: why is there an empty string at the end of the result list?
in
¶Verify whether a letter or a substring occures anywhere inside a string.
The result is a boolean value (True
or False
.)
print('p' in greet)
print('P' in greet)
print('Python' in greet)
if 'P' in greet:
print('Contains a letter P!')
==
¶Perform a case-sensitive equality check between two strings.
if word2 == 'Python':
print('It was Python.')
else:
print('It was not Python.')
Task: request the name, birth year, email address and spoken languages of the user. The spoken languages are requested as a string, separated by commas.
Check whether the following validation rules are matched. If any of the data is invalid, display an error message and request a repeated entry of the data.
@
letter and must end with a elte.hu
domain.When the data was given successfully, trim any unncceseary whitespaces and display it in a corrected format:
import datetime
def valid_name(name):
name = name.strip()
return len(name.split(' ')) >= 2
def valid_birthyear(year):
year = year.strip()
try:
year_num = int(year)
return year_num >= 1900 and year_num <= 2019
except:
return False
def valid_email(email):
email = email.strip()
return '@' in email and email.endswith('elte.hu')
def format_name(name):
return name.strip().title()
def format_age(year):
now = datetime.datetime.now()
age_max = now.year - int(year)
age_min = max(age_max - 1, 0)
if age_max != age_min:
return str(age_min) + "/" + str(age_max)
else:
return str(age_max)
def format_email(email):
return email.strip().lower()
def format_langs(langs):
langs = langs.strip().split(',')
langs = list(map(str.strip, langs))
return langs
name = input("Name: ")
while not valid_name(name):
print("Incorrect format for name.")
name = input("Name: ")
birthyear = input("Birth year: ")
while not valid_birthyear(birthyear):
print("Incorrect format for birth year.")
birthyear = input("Birth year: ")
email = input("Email: ")
while not valid_email(email):
print("Incorrect format for email.")
email = input("Email: ")
langs = input("Spoken languages: ")
print("Name: %s" % format_name(name))
print("Birth year: %s (age: %s)" % (birthyear, format_age(birthyear)))
print("Email: %s" % format_email(email))
print("Languages: %s" % format_langs(langs))