Strings adalah text yang dapat diberikan tanpa kutip, diantara tanda kutip (quote) itu adalah: single (
'
), double ("
), and triple ('''
or """
).Displaying and Receiving String Input.
1) To display a string on the screen, you can use the
print
function. For instance:s= "apa kabar"
print(s)
output: apa kabar
2) The
input
function:name= input("Tulis nama: ")
print(name)
Output: >>> name= input("Tulis nama: ")
Tulis nama: atun
>>> print(name)
atun
>>>
3) The equality operator (
==
):sentence= "kucing warna coklat"
k= "kucing"
if k== sentence: print("identical")
Output: syntax error
Note:
print
statement is only executed if the condition k == sentence
is true.Untuk mem-fix "syntax error," coba coding ini:
sentence="kucing warna coklat"
k="kucing"
if k== sentence:
print("equal")
else:
print("not equal")
Not: still doesn't work
---------------------------------------------------------------
python string slice
s= "Apa Kabar"
print(s)
Output: Apa Kabar
print(s[0])
Output: A
print(s[1])
Output : P
String Slicing in Python
s= "Apa Kabar"
print(s)
output: Apa Kabar
print(s[0])
output: A
print(s[1])
output: p
print(s[4])
output: K
More examples:
>>> s = "Hello Python"
>>> print(s[0:2])
He
>>> print(s[0:1])
H
>>> print(s[0:3])
Hel
>>> print(s[0:4])
Hell
>>> print(s[0:5])
Hello
>>> print(s[0:2])
He
>>> print(s[6:])
Python
>>>
No comments:
Post a Comment