#๐Ÿ”’ Explain string operations...

282 messages ยท Page 1 of 1 (latest)

timber glade
#

It really doesn't make sense to me, and when astring = "Hello world!" but then when print(astring[3:7]) happens, I don't understand why you would get lo w

woeful elbowBOT
#

@timber glade

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

leaden carbon
timber glade
#

I really dont understand it so, l o

leaden carbon
#

do you know what [3:7] does?

timber glade
#

starts at 3 stops at 7

leaden carbon
#

yes, but the stop is "exclusive"

#

meaning that index will not be included

#

so we get whatever is in index 3, 4, 5, 6

#

l o w

timber glade
#

ok thanks!

leaden carbon
#

!e

astring = "Hello world!"
print(*[i % 10 for i in range(len(astring))])
print(*astring)

woeful elbowBOT
leaden carbon
#

these are the index positions of the string

#

I wrapped it around for 10 and 11 just so it would line up properly

timber glade
#

yeah lo w makes sense now

#

I dont understand this one though:

# Length should be 20
print("Length of s = %d" % len(s))

# First occurrence of "a" should be at index 8
print("The first occurrence of the letter a = %d" % s.index("a"))

# Number of a's should be 2
print("a occurs %d times" % s.count("a"))

# Slicing the string into bits
print("The first five characters are '%s'" % s[:5]) # Start to 5
print("The next five characters are '%s'" % s[5:10]) # 5 to 10
print("The thirteenth character is '%s'" % s[12]) # Just number 12
print("The characters with odd index are '%s'" %s[1::2]) #(0-based indexing)
print("The last five characters are '%s'" % s[-5:]) # 5th-from-last to end

# Convert everything to uppercase
print("String in uppercase: %s" % s.upper())

# Convert everything to lowercase
print("String in lowercase: %s" % s.lower())

# Check how a string starts
if s.startswith("Str"):
    print("String starts with 'Str'. Good!")

# Check how a string ends
if s.endswith("ome!"):
    print("String ends with 'ome!'. Good!")

# Split the string into three separate strings,
# each containing only a word
print("Split the words of the string: %s" % s.split(" "))```
woeful elbowBOT
#

Hey @timber glade!

Please edit your message to use a code block

Add a py after the three backticks.

```py
print('Hello, world!')
```

This will result in the following:

print('Hello, world!')```
leaden carbon
#

There's a lot here. What specifically are you referring to?

#

Code is best understood broken down 1 line at a time

timber glade
#

for s; im supposed to change it to the correct string

leaden carbon
#

This seems more like a puzzle that you have to work out based on what the expected result is

leaden carbon
#

So what have you tried so far?

timber glade
#

All I've done is change the string into 20 characters like this:
"Hey there! what shou"

#

I honestly dont know what im doing

leaden carbon
#

Well there's definitely clues scattered throughout the code

#

like what the first 3 characters and last 4 characters should be

#

and the character in index 8 should be a

#

and the string should be 3 words

timber glade
leaden carbon
#
# First occurrence of "a" should be at index 8
print("The first occurrence of the letter a = %d" % s.index("a"))
#

It says it

timber glade
#

I think I just need to learn the concept of strings...

leaden carbon
#

a string is just a collection of text characters

timber glade
#

But like, the tuples!!

leaden carbon
#

what do tuples have to do with strings?

timber glade
#

like these things %d

leaden carbon
#

those are for string formatting

#

using % to do it is actually a very old-school method

#

no one really uses that anymore

#

!e

name = 'zEE'

print("Hello my name is %s" % name)
woeful elbowBOT
leaden carbon
#

it simply lets you replace part of the string with a variable

#

so the %s becomes name

timber glade
#

I thought python was supposed to be easy ๐Ÿ˜ฆ

leaden carbon
#

What about that are you unsure of?

#

Also easy is relative

timber glade
#

the start stop rest stuff

#

and that dumb excerise

leaden carbon
#

just because the exercise is dumb doesn't mean python is hard

#

you're going to find good and bad learning material

#

bad learning material will make things seem harder to learn

timber glade
#

is brocode bad material

leaden carbon
#

I'm not sure, I've never used it

timber glade
leaden carbon
leaden carbon
timber glade
#

Interesing...

#

now it's conditions so i think im fine >:D

leaden carbon
#

but it's good that you're asking questions about the material

#

that's a good way to learn

timber glade
#

๐Ÿ™‚

#

also whats a in operator

#

like ```if name in````

leaden carbon
#

in lets you check for "membership"

#

so if you want to know if a string contains a specific letter or word

#

!e

sentence = 'hello nice to meet you'

if 'nice' in sentence:
    print("aww")
woeful elbowBOT
timber glade
#

is it like

print("Hi John or Rick!")```
leaden carbon
#

no, == will compare the entire string

#

in is used for searching within the string

leaden carbon
timber glade
#

Then what's this?

if name in ["John", "Rick"]:
    print("Your name is either John or Rick.")```
leaden carbon
#

my example above was using in on a string, which looks for a "substring"

#

but using in on a list will check for a member of the list

#

so this is checking if name is either John or Rick

timber glade
#

that makes sense ๐Ÿ˜„

leaden carbon
#

!e

letters = 'abcdef'

if 'e' in letters:
    print("e is found")
woeful elbowBOT
timber glade
#

Ok

leaden carbon
#
suits = ['hearts', 'diamonds', 'spades', 'clubs']
choice = input("What suit would you like to play?")

if choice in suits:
    print("Very well!")
else:
    print("Not valid!")
timber glade
#

How come they did x == 2 instead of x = 2 here?

if x == 2:
    print("x equals two!")
else:
    print("x does not equal to two.")```
woeful elbowBOT
#

Hey @timber glade!

Please edit your message to use a code block

Add a py after the three backticks.

```py
print('Hello, world!')
```

This will result in the following:

print('Hello, world!')```
leaden carbon
#

and = is always used for assignment

timber glade
#

ok

leaden carbon
#

they mean different things

timber glade
#
y = [1,2,3]
print(x == y) # Prints out True
print(x is y) # Prints out False```
Hows x is y false, they look exactly the same!!
leaden carbon
#

== and is are not the same thing

#

== is "equality". If the 2 things are equal

#

is checks if two objects are the same object

#

this concept starts to get a bit more advanced

#

but even though x and y hold the same numbers, they're still different lists

#

if I append to x, y will not change

timber glade
#

so like x = 1
y = x
so y is x is true?

leaden carbon
timber glade
#

๐Ÿ˜ฆ

leaden carbon
#

!e

x = (1,2,3)
y = (1,2,3)
print(x == y) # Prints out True
print(x is y) # Prints out False
woeful elbowBOT
timber glade
#

huh

leaden carbon
#

using your example above, is will be True with tuples

#

I really recommend this article

#

it will clear up a lot of things

timber glade
#
for x in range(5):
    print(x)

# Prints out 3,4,5
for x in range(3, 6):
    print(x)

# Prints out 3,5,7
for x in range(3, 8, 2):
    print(x))``` how does this print out 3,5,7?
leaden carbon
#

the 3rd number in the range is the "step"

#

so it's counting from 3 to 8, by 2s

#

3, 5, 7

timber glade
heavy elk
timber glade
#

Also, what in the world is this?


count = 0
while True:
    print(count)
    count += 1
    if count >= 5:
        break

# Prints out only odd numbers - 1,3,5,7,9
for x in range(10):
    # Check if x is even
    if x % 2 == 0:
        continue
    print(x)```
woeful elbowBOT
#

Hey @timber glade!

Please edit your message to use a code block

Add a py after the three backticks.

```py
print('Hello, world!')
```

This will result in the following:

print('Hello, world!')```
heavy elk
#

which part

timber glade
#

the 2nd

heavy elk
#

which part of it is confusing

timber glade
#

oh wait isnt continuing skipping the number

heavy elk
#

continue skips the current iteration yes

timber glade
#

nvm i got it

leaden carbon
#

things won't make sense until they do

heavy elk
#

so true

timber glade
#

Where are they looping with else?

while(count<5):
    print(count)
    count +=1
else:
    print("count value reached %d" %(count))

# Prints out 1,2,3,4
for i in range(1, 10):
    if(i%5==0):
        break
    print(i)
else:
    print("this is not printed because for loop is terminated because of break but not due to fail in condition")```
leaden carbon
#

for/else is a pretty unique scenario that isn't used too often

heavy elk
#

for/else and while/else are super niche

leaden carbon
#

the else only runs if the loop finishes without breaking

timber glade
#

like crashing?

heavy elk
#

break

leaden carbon
#

it can be useful if you're searching for something in a list

#

and you want to know whether or not it was found

heavy elk
#

if it crashed the code wouldnt continue at all

leaden carbon
#

break exits from a loop

timber glade
#

whats the i%5 for too

leaden carbon
#

% is "modulo"

#

it performs remainder division

heavy elk
#

its arbitrary to demonstrate the use of for/else

#

its saying if the current number is divisible by 5, break

leaden carbon
#

if the result of modulo is 0, it means the number divided evenly into the other

heavy elk
#

and thereby show the use of for/else

timber glade
heavy elk
#

what about em

timber glade
#

so 5/5 is 0 since its divisible

leaden carbon
#

yes

#

but 6/5 would have 1 leftover

heavy elk
#

for a % b you take the closest multiple of b that's less than a and then the result is whatever is left over

leaden carbon
#

!e

for i in range(10):
    print(i % 3)
woeful elbowBOT
leaden carbon
#

it's incredibly useful to wrap things around

heavy elk
#

^ yes, anything "cyclical" will use modulo

leaden carbon
#

let's say I'm playing Monopoly with 4 friends and the game is on "turn 47"

#

I can quickly find out whose turn it is by getting the remainder of 47 % 4

timber glade
#
    951, 402, 984, 651, 360, 69, 408, 319, 601, 485, 980, 507, 725, 547, 544,
    615, 83, 165, 141, 501, 263, 617, 865, 575, 219, 390, 984, 592, 236, 105, 942, 941,
    386, 462, 47, 418, 907, 344, 236, 375, 823, 566, 597, 978, 328, 615, 953, 345,
    399, 162, 758, 219, 918, 237, 412, 566, 826, 248, 866, 950, 626, 949, 687, 217,
    815, 67, 104, 58, 512, 24, 892, 894, 767, 553, 81, 379, 843, 831, 445, 742, 717,
    958, 609, 842, 451, 688, 753, 854, 685, 93, 857, 440, 380, 126, 721, 328, 753, 470,
    743, 527
]

# your code goes here
for number in numbers:``` 
I'm supposed to stop the code after 237 and this is the solution:
```numbers = [
    951, 402, 984, 651, 360, 69, 408, 319, 601, 485, 980, 507, 725, 547, 544,
    615, 83, 165, 141, 501, 263, 617, 865, 575, 219, 390, 984, 592, 236, 105, 942, 941,
    386, 462, 47, 418, 907, 344, 236, 375, 823, 566, 597, 978, 328, 615, 953, 345,
    399, 162, 758, 219, 918, 237, 412, 566, 826, 248, 866, 950, 626, 949, 687, 217,
    815, 67, 104, 58, 512, 24, 892, 894, 767, 553, 81, 379, 843, 831, 445, 742, 717,
    958, 609, 842, 451, 688, 753, 854, 685, 93, 857, 440, 380, 126, 721, 328, 753, 470,
    743, 527
]

# your code goes here
for number in numbers:
    if number == 237:
        break

    if number % 2 == 1:
        continue```
How?!?!
leaden carbon
#
players = ['jim', 'sally', 'ron', 'timmy']
turn = 47

current_player = players[turn % len(players)]
heavy elk
#

it does exactly nothing

leaden carbon
timber glade
#

Why is if number % 2 ==1: continue
is there

leaden carbon
#

there's also nothing being printed so there's no meaningful output

leaden carbon
heavy elk
timber glade
#

So nothing?

heavy elk
timber glade
#

๐Ÿ˜ 

heavy elk
#

lol

timber glade
#

I don't like this website

leaden carbon
#

seeing bad code and understanding that it's bad is still helpful

timber glade
#

But, how am I supposed to know if it's bad without the pros showing me...

leaden carbon
#

experiment

#

put that code in an editor and run it

#

change the part you're curious about and observe results

#

print() will be your best friend to better understand what's happening

#

print everything

#

I still do, 7 years later

heavy elk
#

first, maybe try to rationalize it yourself

#

dont just say "that feels wrong" explain why it is

#

if you can't, thats when you ask

#

but always at least try

timber glade
#

ok

leaden carbon
#

python will tell you very quickly if things are right or wrong

heavy elk
leaden carbon
#

Who says I'm smarter?

#

today I asked chatgpt how to use github

timber glade
heavy elk
#

i guess this is confirmation bias but i have seen many questions you have been able to answer that i have not but not the other way round

leaden carbon
#

Hard to say. Learning "the language" and learning "what you can do with the language" are two different things

leaden carbon
heavy elk
leaden carbon
#

I don't really touch questions about discord.py, numpy, pandas, ML, etc

timber glade
#

did you use python with combobreakers?

leaden carbon
#

combo breakers?

#

do you mean Combo Devils?

heavy elk
timber glade
#

combo devils

#

combobreakers is the tournament

leaden carbon
#

but yes, I built our asset pipeline with python

timber glade
leaden carbon
#

We need a seamless way to manage all of our assets, animations, characters, etc

timber glade
#

i literally only know pygame and love2d ๐Ÿ˜ญ

leaden carbon
#

Our artists/animators work in Autodesk Maya

#

Let's say they do an animation with Character A. We need a way to send their work into the game engine and hook it up

timber glade
#

isnt that for Java

leaden carbon
#

nothing to do with compiling

#

1 sec, let me open maya ๐Ÿ˜‰

#

Let's say an animator does a 3 hit combo that we want in the game

timber glade
#

woa

leaden carbon
#

We actually need these sliced up into 3 separate files for gameplay reasons

timber glade
#

Did they do that in SF?

#

also do you guys use C++

#

or C

#

or C#

leaden carbon
#

This is a custom tool I built for our animators

#

It lets them create animation clips from a single animation scene

timber glade
#

this is python?

leaden carbon
#

This will properly name, categorize, tag, etc

timber glade
#

looks like java to me

leaden carbon
#

so they just need to hit the export button and all the correct data gets sent to the game engine

heavy elk
#

any language can do anything any other language can do

leaden carbon
heavy elk
#

you can't tell what something was made with from visuals

#

you can even write this in pure math, theoretically

timber glade
#

you cant tell if somethings from C++?

heavy elk
#

wouldnt be able to tell

#

no not at all

#

its all different ways of doing the same thing

leaden carbon
timber glade
#

C++ is so hard anyways, why does it take so long to draw a triangular prism ๐Ÿ˜ญ

heavy elk
#

it all gets compiled to the same ones and zeroes

heavy elk
#

i thought unreal was C++

leaden carbon
#

lol yes sorry, mistyped

heavy elk
#

lol

timber glade
#

fortnite engine ๐Ÿคฉ

#

one day i wanna make a game

leaden carbon
# leaden carbon

but yes, this is just one example of tools we can create to improve the workflow of our artists

#

I have all sorts of things to improve the lives of the animators

#

a library tool for loading in past work

#

a transfer tool to batch copy an animation from one character to another

heavy elk
#

making a game would be cool but i am too much of a perfectionist to even start

leaden carbon
#

which makes it easier to transfer animations from one to another

leaden carbon
#

I also build the control rigs for our characters

timber glade
#

whats a control rig

#

is it also in python?

leaden carbon
#

I use python to automate some of the processes

#

but it's all done in Maya

#

a control rig lets an animator interact with the model

timber glade
#

One day i wanna be as smart as you

leaden carbon
#

when we create a model, we create it in "A-pose"

timber glade
#

Maya seems interesting

leaden carbon
#

but it's basically a statue, it has no flexibility

#

I build the skeleton that drives the model

heavy elk
#

i used Maya in high school, i did not realize how tedious modeling / texturing is

leaden carbon
#

and the control rig for interacting with the skeleton

#

If I go into X-Ray mode, we can see the skeleton inside

timber glade
#

why not blender

leaden carbon
timber glade
#

even like

#

fall guys?!?

leaden carbon
#

every major game studio uses maya

timber glade
#

๐Ÿ˜ฎ

leaden carbon
#

many games do use blender

timber glade
#

they used unity

leaden carbon
#

but it's often hobbyists who learned it on their own

#

most schools will teach Maya since that's what's mainly used in the industry

leaden carbon
timber glade
#

Interesintg..

leaden carbon
#

but also yeah, it took me years to develop these skills

#

I started out with very basic scripts

woeful elbowBOT
#
Python help channel closed for inactivity

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.