lua 7

Chapter 7 — Strings (📝 Working with Text)

If numbers are for math, strings are for words.

Almost every app uses strings.

  • User names

  • Passwords

  • Chat messages

  • Emails

  • Search bars

  • File names

  • URLs

If it looks like text, it's probably a string.


🎯 Mission

Imagine you're making WhatsApp.

A user sends:

Hello!

That's a string.


🟢 LEVEL 1 — Survival


Q1

Which of these is a string?

A)

100

B)

true

C)

"Hello"

D)

nil

Answer: C

💡 Text inside quotes is a string.


Q2

Which is NOT a string?

A)

"Lua"

B)

"123"

C)

123

D)

"Hi"

Answer: C

💡 No quotes = number.


Q3

name = "Pramod"

print(name)

Output?

A)

Pramod

B)

name

C)

"Pramod"

D) Error

Answer: A


Q4

Which is valid?

A)

city = Bangalore

B)

city = "Bangalore"

C)

city = Bangalore()

D)

city = {}

Answer: B


Q5

What are quotes (" ") mainly used for?

A) Numbers

B) Strings

C) Tables

D) Loops

Answer: B


Q6

print("Lua")

Output?

A)

Lua

B)

"Lua"

C) Error

D) nil

Answer: A


🟡 LEVEL 2 — Think Like a Programmer


Q7

first = "Pramod"
last = "Tiwari"

print(first .. last)

Output?

A)

PramodTiwari

B)

Pramod Tiwari

C)

Pramod..Tiwari

D) Error

Answer: A

💡 .. joins strings.


Q8

first = "Pramod"
last = "Tiwari"

print(first .. " " .. last)

Output?

A)

Pramod Tiwari

B)

PramodTiwari

C)

Pramod..Tiwari

D) Error

Answer: A


Q9

Which operator joins strings?

A)

+

B)

&

C)

..

D)

*

Answer: C

⚠️ Beginners often try +. Lua uses ...


Q10

print("Hello" .. " World")

Output?

A)

Hello World

B)

HelloWorld

C) Error

D)

World Hello

Answer: A


Q11

name = "Lua"

print(#name)

Output?

A) 2

B) 3

C) 4

D) Error

Answer: B

💡 # gives the length.


Q12

print(#"Programming")

How many characters?

A) 9

B) 10

C) 11

D) 12

Answer: C


🟠 LEVEL 3 — Real-Life Programming


Q13

A website shows:

Welcome Pramod

Best code?

A)

print("Welcome")
print(name)

B)

print("Welcome " .. name)

C)

print(name + "Welcome")

D)

print(name)

Answer: B


Q14

You want:

Score: 100

Best approach?

A)

print("Score: " .. score)

B)

print(score + "Score")

C)

print(score)

D)

print("Score")

Answer: A


Q15

A game says:

Game Over

This is...

A) Number

B) String

C) Boolean

D) Table

Answer: B


Q16

You're building a login screen.

Username should be stored as...

A) Number

B) String

C) Boolean

D) Loop

Answer: B


Q17

A chatbot receives messages.

Each message is...

A) String

B) Table

C) Function

D) Number

Answer: A


Q18

A filename like:

photo.jpg

is usually stored as...

A) Number

B) String

C) Boolean

D) Loop

Answer: B


🔴 LEVEL 4 — Bug Hunter


Q19

name = Pramod

Problem?

A) Missing quotes

B) Missing loop

C) Missing table

D) Nothing

Answer: A


Q20

print("Hello" + "World")

Problem?

A) Should use ..

B) Missing if

C) Missing end

D) Nothing

Answer: A


Q21

name = "Lua"

print(name + 1)

Problem?

A) Nothing

B) Can't add text and number with +

C) Missing quotes

D) Missing loop

Answer: B


Q22

word = "Cat"

print(#word)

Output?

A) 2

B) 3

C) 4

D) Error

Answer: B


Q23

print("Age: " .. 20)

Output?

A)

Age: 20

B) Error

C)

20Age

D)

Age:

Answer: A

💡 Lua automatically converts the number when concatenating with ...


Q24

Which is correct?

A)

print("Hello" .. "Lua")

B)

print("Hello" + "Lua")

C)

print("Hello" * "Lua")

D)

print("Hello" & "Lua")

Answer: A


🟣 LEVEL 5 — Boss Battle 👑


Q25

How do you join two strings in Lua?

A)

+

B)

..

C)

&

D)

=

Answer: B


Q26

How do you find the length of a string?

A)

length(name)

B)

#name

C)

size(name)

D)

count(name)

Answer: B


Q27

first = "Chat"
second = "GPT"

print(first .. second)

Output?

A)

Chat GPT

B)

ChatGPT

C)

GPTChat

D) Error

Answer: B


Q28

Which is most likely to be a string?

A) Health

B) Coins

C) Email Address

D) Player Level

Answer: C


Q29

Which operator is used most often with strings?

A)

+

B)

-

C)

..

D)

/

Answer: C


Q30 — Final Boss 🏆

You're building a game.

You want to display:

Welcome Pramod!
Level: 25
Coins: 1000

What's your first thought?

A) Store everything as numbers.

B) Combine strings with variables using ...

C) Use only loops.

D) Use only tables.

Answer: B

💡 Almost every app you use creates messages by joining text with variables. That's why string concatenation (..) is one of the most frequently used Lua features.


🧠 Pareto Cheat Sheet

SituationUse
Text"Hello"
Join strings..
Length of string#text
Store a name"Pramod"
Store an email"abc@gmail.com"
Display message"Welcome " .. name

🏆 The 80/20 Rule for Strings

If you remember only three things from this chapter, remember these:

  1. Text goes inside quotes"Hello"

  2. Join text with .."Hi " .. name

  3. Get length with ##name

Those three cover most of the string operations you'll use in beginner and intermediate Lua programs.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.