4. Strings - tomaslt99/Python-language-tutorials GitHub Wiki
3 major data, string values:
- Plain Text values. quotation mark is needed only for string.
- Number values, quotation mark are not needed for number string.
- Boolean value True or False
Code:
# 3 major data, string values:
# 1) Plain Text values. Quotation mark needed only for string.
character_name = "John"
# 2) Number values, quotation mark not needed for number string.
character_age = "35.27"
# Boolean value True or False
is_male = False
print("There once was a man " + character_name + ",")
print("He was year " + character_age + " old.")
Output:
There once was a man John,
He was year 35.27 old.
Updated new value for character_name.
Code:
# Updated new value for character_name.
character_name = "Tom"
print("He really liked the name " + character_name + ".")
Output:
He really liked the name Tom.
Code:
# Print examples
print("First\nLast")
print("First\"Last")
print("First\Last")
phrase = "First Last"
print(phrase)
phrase = "First Last"
print(phrase + "is cool")
Output:
First
Last
First"Last
First\Last
First Last
First Last is cool
Function with strings
Code:
# Example write text in lower case
phrase = "First Last"
print(phrase.lower())
# Example write text in upper case
phrase = "First Last"
print(phrase.upper())
# Example true or false, write "True"if text is uppercase and wiseversa
phrase = "FIRSTirst Last"
print(phrase.isupper())
Output:
first last
FIRST LAST
False
Example Count allthe characters and prints the count.
Code:
phrase = "Count allthe characters and prints
the count."
print(len(phrase))
Output:
46
#Eexample: Find out X character in a string. Counting start from 0, 1st Character in string is 0 count. "Find" 0123
Code:
phrase = "Find out 1st character in a string"
print(phrase[0])
phrase = "Find out 3st character in a string"
print(phrase[3])
Output:
F
d
Index Function Index function tells where specific character or string is located. Example:
Code:
phrase = "Find where is specific character in a string"
print(phrase.index("F"))
Output:
0
Example:
Code:
phrase = "Replace word in a sentence. Tom"
print(phrase.replace("Tom","Mike"))
Output:
Replace word in a sentence. Tom