#๐ Explain string operations...
282 messages ยท Page 1 of 1 (latest)
@timber glade
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.
what do you think it should be?
I really dont understand it so, l o
do you know what [3:7] does?
starts at 3 stops at 7
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
ok thanks!
!e
astring = "Hello world!"
print(*[i % 10 for i in range(len(astring))])
print(*astring)
:white_check_mark: Your 3.13 eval job has completed with return code 0.
001 | 0 1 2 3 4 5 6 7 8 9 0 1
002 | H e l l o w o r l d !
these are the index positions of the string
I wrapped it around for 10 and 11 just so it would line up properly
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(" "))```
Hey @timber glade!
Add a py after the three backticks.
```py
print('Hello, world!')
```
This will result in the following:
print('Hello, world!')```
There's a lot here. What specifically are you referring to?
Code is best understood broken down 1 line at a time
for s; im supposed to change it to the correct string
This seems more like a puzzle that you have to work out based on what the expected result is
So what have you tried so far?
All I've done is change the string into 20 characters like this:
"Hey there! what shou"
I honestly dont know what im doing
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
how do you know that...
# First occurrence of "a" should be at index 8
print("The first occurrence of the letter a = %d" % s.index("a"))
It says it
I think I just need to learn the concept of strings...
a string is just a collection of text characters
But like, the tuples!!
what do tuples have to do with strings?
like these things %d
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)
:white_check_mark: Your 3.13 eval job has completed with return code 0.
Hello my name is zEE
it simply lets you replace part of the string with a variable
so the %s becomes name
I thought python was supposed to be easy ๐ฆ
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
is brocode bad material
I'm not sure, I've never used it
is learnpython.org bad material
Also this was just because you didn't understand what was happening
I'm not sure either, but it might be a bit outdated if they're using % for string formatting
but it's good that you're asking questions about the material
that's a good way to learn
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")
:white_check_mark: Your 3.13 eval job has completed with return code 0.
aww
is it like
print("Hi John or Rick!")```
this was True because nice was found inside the sentence
Then what's this?
if name in ["John", "Rick"]:
print("Your name is either John or Rick.")```
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
this is the equivalent
that makes sense ๐
!e
letters = 'abcdef'
if 'e' in letters:
print("e is found")
:white_check_mark: Your 3.13 eval job has completed with return code 0.
e is found
Ok
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!")
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.")```
Hey @timber glade!
Add a py after the three backticks.
```py
print('Hello, world!')
```
This will result in the following:
print('Hello, world!')```
== is always used for comparison
and = is always used for assignment
ok
they mean different things
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!!
== 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
so like x = 1
y = x
so y is x is true?
This is where the concept of is can be quite complicated. Not all datatypes behave this way
๐ฆ
!e
x = (1,2,3)
y = (1,2,3)
print(x == y) # Prints out True
print(x is y) # Prints out False
:white_check_mark: Your 3.13 eval job has completed with return code 0.
001 | True
002 | True
huh
using your example above, is will be True with tuples
I really recommend this article
it will clear up a lot of things
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?
the 3rd number in the range is the "step"
so it's counting from 3 to 8, by 2s
3, 5, 7

3 = 3
3 + 2 = 5
3 + 2 + 2 = 7
3 + 2 + 2 +2 is more than 8, so this one doesnt count and you stop
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)```
Hey @timber glade!
Add a py after the three backticks.
```py
print('Hello, world!')
```
This will result in the following:
print('Hello, world!')```
which part
the 2nd
which part of it is confusing
oh wait isnt continuing skipping the number
continue skips the current iteration yes
nvm i got it
things won't make sense until they do
so true
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")```
for/else is a pretty unique scenario that isn't used too often
for/else and while/else are super niche
the else only runs if the loop finishes without breaking
like crashing?
break
it can be useful if you're searching for something in a list
and you want to know whether or not it was found
if it crashed the code wouldnt continue at all
break exits from a loop
whats the i%5 for too
its arbitrary to demonstrate the use of for/else
its saying if the current number is divisible by 5, break
if the result of modulo is 0, it means the number divided evenly into the other
and thereby show the use of for/else
remainders...
what about em
so 5/5 is 0 since its divisible
for a % b you take the closest multiple of b that's less than a and then the result is whatever is left over
!e
for i in range(10):
print(i % 3)
:white_check_mark: Your 3.13 eval job has completed with return code 0.
001 | 0
002 | 1
003 | 2
004 | 0
005 | 1
006 | 2
007 | 0
008 | 1
009 | 2
010 | 0
it's incredibly useful to wrap things around
^ yes, anything "cyclical" will use modulo
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
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?!?!
players = ['jim', 'sally', 'ron', 'timmy']
turn = 47
current_player = players[turn % len(players)]
i dont know what the point of the second if statement is
it does exactly nothing
what about it are you unsure of?
Why is if number % 2 ==1: continue
is there
there's also nothing being printed so there's no meaningful output
just to confuse you
.
So nothing?
yes
๐
lol
I don't like this website
seeing bad code and understanding that it's bad is still helpful
But, how am I supposed to know if it's bad without the pros showing me...
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
dont second guess yourself. you saw the second if statement and intuited that it did nothing but asked us anyway
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
ok
python will tell you very quickly if things are right or wrong
โน๏ธ you've been doing this the same amount of time as me and you are this much smarter? demoralizing
What year was it when you "knew" python?
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
Hard to say. Learning "the language" and learning "what you can do with the language" are two different things
Yeah, I don't chime in on things way above me
i dont think it even takes a year to reach full comfort with a single language, conversely if you mean "knew" programming i dont think there exists a person who can say that entirely
I don't really touch questions about discord.py, numpy, pandas, ML, etc
did you use python with combobreakers?
what do you mean by this
oops
combo devils
combobreakers is the tournament
the team is actually there right now hosting a booth ๐
but yes, I built our asset pipeline with python
whats that
We need a seamless way to manage all of our assets, animations, characters, etc
i literally only know pygame and love2d ๐ญ
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
like a data compiler?
isnt that for Java
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
woa
We actually need these sliced up into 3 separate files for gameplay reasons
This is a custom tool I built for our animators
It lets them create animation clips from a single animation scene
this is python?
This will properly name, categorize, tag, etc
looks like java to me
so they just need to hit the export button and all the correct data gets sent to the game engine
any language can do anything any other language can do
Yes
you can't tell what something was made with from visuals
you can even write this in pure math, theoretically
you cant tell if somethings from C++?
wouldnt be able to tell
no not at all
its all different ways of doing the same thing
Yes, the game is in Unity, which is C++ C#
C++ is so hard anyways, why does it take so long to draw a triangular prism ๐ญ
it all gets compiled to the same ones and zeroes
isnt unity C#
i thought unreal was C++
lol yes sorry, mistyped
lol
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
making a game would be cool but i am too much of a perfectionist to even start
that seems real hard
It can be, but I was smart about how I set up our pipeline. We reuse character skeletons within the cast
which makes it easier to transfer animations from one to another
your devs are ๐ง
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
One day i wanna be as smart as you
Maya seems interesting
but it's basically a statue, it has no flexibility
I build the skeleton that drives the model
i used Maya in high school, i did not realize how tedious modeling / texturing is
and the control rig for interacting with the skeleton
If I go into X-Ray mode, we can see the skeleton inside
why not blender
maya is industry standard, it's what everyone on the team uses in their work
every major game studio uses maya
๐ฎ
they used unity
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
that doesn't necessarily mean maya or blender was used
Interesintg..
but also yeah, it took me years to develop these skills
I started out with very basic scripts
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.