Saturday, December 16, 2023

Python Basics - Operasi Strings - Module 1 - Part 3

Strings deretan dari karakter, setiap string ada tanda petiknya: ' ' atau " ". Misalnya 'Michael Jackson' atau "Michael ackson"




Image 1 - Cherry, just illustration

String can be space or digits: '1 2 3 4'
Strings can also be special characters: '#$@%'

Name= 'M i c h a e l        J  a  c  k   s   o   n'
index:    0 1 2 3 4 5 6  7  8 9 10 11 12 13 14
Name[0]: M
Name[6]: l
Name[13]: o

We can also use negative indexes.



Image 2 - Bunga liar, hanya ilustrasi

Name= 'M   i   c   h  a  e   l'
index:    -7 -6 -5 -4 -3 -2 -1

Name[-6]: i

Strings: slicing
Name[-7:-5]: Mic

tuple is a built-in data type in Python, used to store collections of data. It is one of the four built-in data types in Python used to store collections of data, the other three being:
- List
- Set
- and Dictionary. 



Image 3 - Jambu bol, siap untuk dikonsumsi

A tuple is a collection which is ordered and unchangeable. Tuples are written with round brackets.


String: escape sequences: backslash (\)

\n represents new line
print('Michael \n is the best')
output: Michael 
is the best



Image 4 - Pantai di pulau Batam.

\t represents tab
print('Michael \t is the best')
output: Michael is the best

\\ to place single backlash \
print('Michael \\ is the best')
output: Michael \ is the best



Image 5 - Jalan menuju ke Barelang

String: Stride
Name ="M  i  c  h a e l"
              0  1 2  3 4 5 6
Name.find('el'):5 

If not in the substring Michael, thus the result: -1
Name.find('&*D'):-1

Quiz:
1) Name= "Michael Jackson"
What is the result of the following: Name[0]
answer: M

2) Name= "Michael Jackson"
Name[-1]
answer: n

3) What is the output of the following: print("AB\nC\nDE")
answer: 
AB
C
DE

4)What is the result of following?
"hello Mike".find("Mike")
answer: 6


JJ 
What is the result of following?
"hello Mike".find("Mike")

What is the output of the following: print("AB\nC\nDE")
JJ

JJJjjj

No comments:

Post a Comment