기초 트레이닝 DAY1~2 (Python, js)

이다형·2023년 10월 23일
0

문자열 출력하기

python

str = input()
print(str)

js

const readline = require('readline');
const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});

let input;

rl.on('line', function (line) {
    input = line;
}).on('close',function(){
    console.log(input)
});

a와 b출력하기

python

a, b = map(int, input().strip().split(' '))
print("a =", a)
print("b =", b)

js

const readline = require('readline');
const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});

let input = [];

rl.on('line', function (line) {
    input = line.split(' ');
}).on('close', function () {
    console.log(`a = ${input[0]}\nb = ${input[1]}`);
});

⭐대소문자 바꿔서 출력하기

python

나의풀이

str = input()

for i in range(len(str)):
    if str[i] == str[i].upper():
        print(str[i].lower(), end="")
    else:
        print(str[i].upper(), end="")

다른사람의 풀이

print(input().swapcase())

파이썬은 대소문자를 구별하여 바꿔주는 swapcase()가 있었다..

js

const readline = require('readline');
const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});

let input = [];

rl.on('line', function (line) {
    input = line
}).on('close', function () {
    const convertedInput = input.split("").map(x => {
        if (x === x.toUpperCase()) {
            return x.toLowerCase();
        } else {
            return x.toUpperCase();
        }
    }).join("");
    
    console.log(convertedInput);
});
  • 문자열 길이
    python : len(str)
    js : str.length

  • 대소문자 변환
    python : str.upper() / str.lower()
    js : str.toUpperCase() / str.toLowerCase()


특수문자 출력하기

python

print("!@#$%^&*(\\'\"<>?:;")

js

const readline = require('readline');
const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});

rl.on('close', function () {
    print("!@#$%^&*(\\\'\"<>?:;")
});
  • 백슬래시 (\)
  • 따옴표(*), 큰따옴표(") 앞 백슬래시(\)를 붙이면 된다

덧셈식 출력하기

python

a, b = map(int, input().strip().split(' '))
print(f"{a} + {b} = {a+b}")

f-string 식은 중괄호안에 변수나 표현식을 넣어서 값을 문자로 변환하여 출력한다.

js

const readline = require('readline');
const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});

let input = [];

rl.on('line', function (line) {
    input = line.split(' ');
}).on('close', function () {
    console.log(`${input[0]} + ${input[1]} = ${+input[0] + +input[1]}`);
});
  • 백틱 `` 안에서 ${} 로 문자 이외의 값을 출력할 수 있다.
  • 변수나 string 앞에 +를 붙이면 정수값이 된다.

문자열 붙여서 출력하기

python

str1, str2 = input().strip().split(' ')
print(str1+str2)

f-string 식은 중괄호안에 변수나 표현식을 넣어서 값을 문자로 변환하여 출력한다.

다른풀이

print(input().replace(' ', ''))

입력 apple pen 의 공백을 치환 replace()

str1, str2 = input().strip().split(' ')
print(str1, str2, sep='')

문자열 둘을 공백없이 병합 sep =

js

const readline = require('readline');
const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});

let input = [];

rl.on('line', function (line) {
    input = line.split(' ');
}).on('close', function () {
    console.log(`${input[0]}${input[1]}`)     
});

문자열 돌리기

python

for a in input():
    print(a)

js

const readline = require('readline');
const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});

let input = [];

rl.on('line', function (line) {
    input = line.split("")
}).on('close',function(){
    input.forEach((x) => console.log(x))
});

홀짝 구분하기

python

a = int(input())

# if a%2 == 1:
#     print(f"{a} is odd")
# else:
#     print(f"{a} is even")

print(f"{a} is {'odd' if a%2 == 1 else 'even'}")

js

const readline = require('readline');
const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});

let input = [];

rl.on('line', function (line) {
    input = line.split(' ');
}).on('close', function () {
    n = Number(input[0]);
    n % 2 === 1 ? console.log(`${n} is odd`) : console.log(`${n} is even`)
});

문자열 겹쳐쓰기

python

def solution(my_string, overwrite_string, s):
    return my_string[:s] + overwrite_string + my_string[s+len(overwrite_string):]

js

function solution(my_string, overwrite_string, s) {
    return (my_string.slice(0,s) + 
            overwrite_string + 
            my_string.slice(s+overwrite_string.length)
           ) 
}

0개의 댓글